天天看点

javaday45_IO流5_装饰者模式、lineNember、字节流

1.内容:

2.装饰设计模式,PersonDemo

package java_Studing_day45_IO流5_装饰设计模式;

public class persondemo {

    public static void main(String[] args) {

// TODO Auto-generated method stub

Person p=new Person();

//p.eat();

NewPerson np=new NewPerson(p);

np.eat();

np.runcar();

System.out.println("**********************************************");

NewPerson2 np2=new NewPerson2();

np2.eat();

    }

}

class Person{

    public void eat(){

System.out.println("吃饭");

    }

 }

//这个类的出现是为了增强Person这个类而出现的                        装饰设计模式

class NewPerson{

    private Person p;

    NewPerson(Person p){ //构造函数传入Person对象进行增强

this.p=p;

}

    public void eat(){

System.out.println("开胃酒");

p.eat();

System.out.println("甜点");

    }

    public void runcar(){

System.out.println("开跑车");

    }

}

//继承设计模式

class NewPerson2 extends Person{

     public void eat(){

System.out.println("开胃酒");

super.eat();

System.out.println("甜点");

    }

}

3.lineNemberReader的使用

package java_Studing_day45_IO流5_装饰设计模式;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.LineNumberReader;

public class lineNumberReader {

    private static final String Line_Menber = System.getProperty("line.separator");//获取系统换行符

    public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

FileWriter fw=new FileWriter("lineNumberReader.txt");

fw.write("sdfsadfeaLine_Men\nbersdgfsdLine_Men\nberrersdfgesdLine_M\nenbernbergrsdfLinen_Menbe\nrerdgsrgLinenMenbe\nrnbersrgsdgLine_Men\nberenbergsewgsdfgs");

fw.flush();

FileReader fr=new FileReader("lineNumberReader.txt");

LineNumberReader lnr=new LineNumberReader(fr);//获取行号的缓冲

String line=null;

lnr.setLineNumber(100);//设置行号开始编号

while((line=lnr.readLine())!=null){

   System.out.println(lnr.getLineNumber()+":"+line);//打印行号和缓存内容

}

lnr.close();

    }

}

4.字节流Demo和常用的缓存方式选择

package java_Studing_day45_IO流5_字节流;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class ByteStreamDemo {

    public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

demo_write();

read();

    }

//读

public static void read() throws IOException{

//创建一个读取流对象,和指定文件关联

FileInputStream fis=new FileInputStream("Bytedemo1.txt");

//第4种,获取刚好大小的数组做容器

System.out.println(fis.available());

byte[] buf=new byte[fis.available()];

fis.read(buf);

System.out.println(new String(buf));

//第3种方式,自定义Byte数组容器当做缓存容器读取

// byte[] buf=new byte[1024];

// int len=0;

// while((len=fis.read(buf))!=-1){

//    System.out.println(new String(buf,0,len));

// }

//第2种方式,系统缓存读

// int ch=0;

// while((ch=fis.read())!=-1){

//    System.out.println((char)ch);

// }

//第1种方式,一次读取一个字节

//int ch=fis.read();

//System.out.println(ch);

fis.close();

    }

//写

   public static void demo_write() throws IOException {

//1.创建字节输出流对象,用于操作文件

FileOutputStream fos=new FileOutputStream("Bytedemo1.txt");

//2.写数据,直接写入到了目的地中

fos.write("abcdefg".getBytes());

//fos.flush();//现在没什么用,只有在缓冲区有用

fos.close();//关闭资源的动作还是要用的

    }

}