021.9 IO流 流总结

###################################################################################
IO流的规律总结:解决的问题,开发中具体使用哪个流对象的问题
1,明确数据源,数据目的
    就是在明确要使用的IO体系。InputStream   OutputStream     Reader       Writer
    需求中做为源:意味着是读
    使用:InputStream    Reader
    
    需求中做为目的:意味着是写
    使用:OutputStream    Writer
    
2,操作的数据是否是纯文本数据
    是,字符流
    否,字节流
    是并且是源,使用Reader
    是并且是目的,Writer
    
3,明确要操作的具体设备。每个设备都有对应的流对象
    源设备:
        硬盘:能操作File的流对象都是。File开头
        键盘:System.in
        内存:数组
        网络:socket流
    目的设备:
        硬盘:能操作File的流对象都是。File开头
        键盘:System.out
        内存:数组
        网络:socket流
        
4,是否需要额外的功能
    是否需要高效:缓冲区,Buffered开头
    是否需要编码转换:转换流

######################################################################################

需求1、通过键盘录入数据,保存到一个文件中
    思路:1)明确源和目的,都有,源:IntputStream  Reader              目的:OutputStream     Writer
        2)明确是否是文本,是文本,源:Reader   目的:Writer
        3)具体设备:源:System.in          目的:硬盘

public static void main(String[] args) throws IOException
{
    InputStream is = System.in;
    FileWriter fw = new FileWriter("myfile\\a.txt");
    byte[] by = new byte[1024];
    int count = 0;
    while((count = is.read(by))!=-1){
        System.out.println(new String(by,0,count));
        fw.write(new String(by), 0, count);
    }
    fw.close();
}
1.1

//但是麻烦,因为明确源是Reader,需要将字节流转成字符流,所以用InputStreamReader

public static void main(String[] args) throws IOException
{
    InputStreamReader isr = new InputStreamReader(System.in);
    FileWriter fw = new FileWriter("myfile\\a.txt",true);
    char[] chr = new char[1024];
    int count = 0;
    while((count = isr.read(chr)) != -1){   //因为是键盘输入不会有末端。
        String s = new String(chr,0,count);
        System.out.println(s);
        fw.write(s);
        fw.flush();
    }
    fw.close();
}
1.2

//需要高效,加入缓冲区

public static void main(String[] args) throws IOException
{
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    FileWriter fw = new FileWriter("myfile\\a.txt",true);
    BufferedWriter bw = new BufferedWriter(fw);
    String line = "";
    while((line = br.readLine()) != null){   //因为是键盘输入不会有末端。
        bw.write(line);
        bw.newLine();
        System.out.println(line);
        bw.flush();
    }
    br.close();
    bw.close();
}
1.3

 

###############################################################################################
需求2:读取文本文件,显示在控制台上
    思路:1)明确源和目的,都有,源:IntputStream  Reader              目的:OutputStream     Writer
        2)明确是否是文本,是文本,源:Reader   目的:Writer
3)具体设备:源:硬盘          目的:System.out

public static void main(String[] args) throws IOException
{
    FileReader fr = new FileReader("myfile\\a.txt");
    OutputStream os = System.out;
    char[] chr = new char[1024];
    int count = 0;
    while((count = fr.read(chr)) != -1){
        String s = new String(chr,0,count);
        byte[] by = s.getBytes();                       //因为OutputStream用的是byte[],所以我转成byte类型
        os.write(by);
    }
    System.out.println("//read over!");
}
2.1
public static void main(String[] args) throws IOException
{
    FileReader fr = new FileReader("myfile\\a.txt");
    OutputStreamWriter osw = new OutputStreamWriter(System.out);
    char[] chr = new char[1024];
    int count = 0;
    while((count = fr.read(chr)) != -1){
        osw.write(chr,0,count);         //没有显示在控制台上,需要flush一下
        osw.flush();
    }

}
2.2
public static void main(String[] args) throws IOException
{
    FileReader fr = new FileReader("myfile\\a.txt");
    BufferedReader br = new BufferedReader(fr);
    OutputStreamWriter osw = new OutputStreamWriter(System.out);
    BufferedWriter bw = new BufferedWriter(osw);
    String line = "";
    while((line = br.readLine()) != null){
        bw.write(line);
//        System.out.println(line);
    }
}
2.3加入缓冲区

 

posted @ 2018-05-20 15:48  Alos403  阅读(224)  评论(0编辑  收藏  举报