第七篇 IO流技术(七)
装饰者模式
package com.zzp.demo; /** * * 实现放大器对声音的放大 * @author java * */ public class DecorateTest01 { public static void main(String[] args) { Person p = new Person(); p.say(); //装饰 Amplifier a = new Amplifier(p); a.say(); } } //1、定义一个接口 interface Say{ void say(); } //2、定义一个person类,实现say类 class Person implements Say{ private int voice = 10; @Override public void say() { System.out.println("人说话的声音:"+ this.getVoice()); } public int getVoice() { return voice; } public void setVoice(int voice) { this.voice = voice; } } //3、定义一个a类,对声音进行放大 class Amplifier implements Say{ private Person p; Amplifier(Person p){ this.p = p; } @Override public void say() { System.out.println("放大100倍后的声音"+ p.getVoice()*100); System.out.println("噪音"); } }
模拟咖啡
package com.zzp.demo; /** * * 模拟咖啡 * 1、抽象组件:需要装饰的抽象对象(接口或抽象父类) * 2、具体组件:需要装饰的对象 * 3、抽象装饰类:包含了对抽象组件的引用以及装饰者共有的方法 * 4、具体装饰类:被装饰的对象 * @author java * */ public class DecorateTest02 { public static void main(String[] args) { Drink coffee = new Coffee(); Drink suger = new Suger(coffee);//装饰 System.out.println(suger.info()+"----"+suger.cost()); Milk milk = new Milk(coffee);//装饰 System.out.println(milk.info()+"----"+milk.cost()); Milk milkSuger = new Milk(suger);//装饰 System.out.println(milkSuger.info()+"----"+milkSuger.cost()); } } //1、抽象组件 interface Drink{ double cost(); String info(); } //2、具体组件 class Coffee implements Drink{ private String name = "原味咖啡"; @Override public double cost() { return 10; } @Override public String info() { return name; } } //3、抽象装饰类 abstract class Decorate implements Drink{ //对抽象组件的引用 private Drink drink; Decorate(Drink drink){ this.drink = drink; } //装饰者共有的方法 @Override public double cost() { // TODO Auto-generated method stub return this.drink.cost(); } @Override public String info() { // TODO Auto-generated method stub return this.drink.info(); } } //4、具体装饰类 class Suger extends Decorate{ Suger(Drink drink) { super(drink); } @Override public double cost() { // TODO Auto-generated method stub return super.cost()*4; } @Override public String info() { // TODO Auto-generated method stub return super.info()+"加入蔗糖"; } } //4、具体装饰类 class Milk extends Decorate{ Milk(Drink drink) { super(drink); // TODO Auto-generated constructor stub } @Override public double cost() { // TODO Auto-generated method stub return super.cost()*10; } @Override public String info() { // TODO Auto-generated method stub return super.info() + "加入牛奶"; } }
加入BufferedInputStream
package com.zzp.demo; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; /** * * 四个步骤:分段读取(BufferedInputStream) * 1、创建源 * 2、选择流 * 3、操作 * 4、释放资源 * @author java * */ public class BufferedTest01 { public static void main(String[] args) { //1、创建源 File src = new File("1.txt");//相对路径 //2、选择流 InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(src)); //3、操作 分段读取 byte[] flush = new byte[1024*10]; //10k一读取 int len = -1;//设置默认长度为-1 while((len = is.read(flush))!= -1){ String str = new String(flush, 0, len);//将字节转化为字符串 System.out.println(str); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally{ if(null != is){ //4、释放资源 try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
BufferedOutputStream
package com.zzp.demo; import java.io.BufferedOutputStream; import java.io.File; import java.io.OutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStream; /** * * 四个步骤:(BufferedOutputStream) * 1、创建源 * 2、选择流 * 3、操作 * 4、释放资源 * @author java * */ public class BufferedTest02 { public static void main(String[] args) { //1、创建源 File dest = new File("dest.txt");//如果不存在,就创建 //2、选择流 OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(dest));//加true表示后面进行拼接,如果为false,则替换之前的内容 //3、操作(写入操作) String msg = "我爱我的国家"; byte[] datas = msg.getBytes();//将字符串转化成字节数组 System.out.println(datas); os.write(datas, 0, datas.length); os.flush();//刷新 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(null != os){ try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
package com.zzp.demo; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.Reader; /** * * 四个步骤:分段读取(文件字符输入缓冲流)BufferedReader * 1、创建源 * 2、选择流 * 3、操作 * 4、释放资源 * @author java * */ public class BufferedTest03 { public static void main(String[] args) { //1、创建源 File src = new File("1.txt");//相对路径 //2、选择流 BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(src)); //3、操作 分段读取 String line = null; while((line = reader.readLine())!=null){ System.out.println(line); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally{ if(null != reader){ //4、释放资源 try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
package com.zzp.demo; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.File; import java.io.OutputStream; import java.io.Writer; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStream; /** * * 四个步骤:(文件字符输出缓冲流) * 1、创建源 * 2、选择流 * 3、操作 * 4、释放资源 * @author java * */ public class BufferedTest04 { public static void main(String[] args) { //1、创建源 File dest = new File("dest.txt");//如果不存在,就创建 //2、选择流 BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter(dest));//加true表示后面进行拼接,如果为false,则替换之前的内容 //3、操作(写入操作) //写法一 String msg = "我爱我的国家,i like my country"; /*char[] datas = msg.toCharArray();//将字符串转化成字符数组 System.out.println(datas); writer.write(datas, 0, datas.length);*/ //写法二 /*writer.write(msg);*/ //写法三 writer.append("我爱我的家乡"); writer.newLine(); writer.append("我爱我的生活"); writer.flush();//刷新 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(null != writer){ try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
package com.zzp.demo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.OutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStream; /** * * 四个步骤:文件拷贝(文件字符输入缓冲流和文件字符输出缓冲流) 1、创建源 2、选择流 3、操作 4、释放资源 * * @author java * */ public class CopyTxt { public static void main(String[] args) { copy("src/com/zzp/demo/CopyTxt.java", "2.txt"); } public static void copy(String srcPath, String destPath) { // 1、创建源 File src = new File(srcPath);// 源 File dest = new File(destPath);// 目的地 // 2、选择流 /*InputStream is = null; OutputStream os = null;*/ try(BufferedReader is = new BufferedReader(new FileReader(src)); BufferedWriter os = new BufferedWriter(new FileWriter(dest));) { /*is = new FileInputStream(src); os = new FileOutputStream(dest, true);*/// 加true表示后面进行拼接,如果为false,则替换之前的内容 // 3、操作(写入操作) String line = null; while ((line = is.readLine()) != null) { os.write(line); os.newLine(); } os.flush();// 刷新 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } /*finally {// 先打开的后关闭 try { if (null != os) { os.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (null != is) { is.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ } }