看完老师关于输入输出流最后的视频后,对老师最后提过的关于如何辨别该用哪个输入输出流的方法觉得很好,凭着看完后的理解,在下面自己针对四种情况作了一下分析,可能并不正确,仅作为思路的评记。
分析一:键盘输入到控制台输出输入输出流分析
源:键盘 Read,是纯文本:FileReader,而System.in是InputStream字节读取流,因此要做字节字符流转换,用到InputStreamReader
InputStreamReader isr = new InputStreamReader(System.in);
是否需要提高效率:是,用到BufferedReader
BufferedReader bfr = new BufferedReader(is);
综合:BufferedReader bfr = new BuffereaReader(new InputStreamReader(System.in));
目的:控制台 Write ,是纯文本:FileWriter,而System.out是字节输出流,因此用到字符到字节转换流:OutputStreamWriter,
OutputStreamWriter osw = new OutputStreamWriter(System.out);
是否需要提高效率:是
BufferedWriter bfw = new BufferedWriter (osw);
综合起来:
BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(System.in));
分析二: 键盘输入,a.txt文件保存
源:Read ,是纯文本:FileRead,而System.in是InputStream字节读取流,因此需要做字节字符流转换,用到InputStreamReader
InputStreamReader isr = new InputStreamReader(Syatem.in);
是否需要提高效率:是
BufferedReader bfr = new BufferedReader(isr);
综合起来:
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
目的:硬盘 Write,是纯文本:FileWriter,
FileWrtiter fw = new FileWriter(“a.txt”);
是否需要提高效率:是
BufferedWriter bfw = new BufferedWriter(fw);
综合起来:
BufferedWriter bfw = new BufferedWriter(new FileWriter(“a.txt”));
分析三:从文件读取数据到控制台上
源:硬盘上的文件 Read,是纯文本FileReader。
FileReader fr = new FileReader(“demo.java”);
是否要提高效率:是
BufferedReader bfr = new BufferedReader(fr);
综合BufferedReader bfr = new BufferedRead(new FileReader(“demo.java”));
目的:控制台 Writer ,是纯文本FileWriter
因为System.out是字节输出流,因此需要用到字符字节转换流OutputStreamWrite
OutputStreamWriter osw = new OutputStreamWriter(System.out);
是否需要提高效率:是
BufferedWriter bfw = new BufferedWriter(osw);
综合BufferedWriter bfw = new BuffferedWriter(new OutputStreamWriter(System.out))
分析四:将c:\demo.bmp文件,赋值到d:\下,输入输出流分析
源:硬盘上的图片:InputSteam,不是纯文本:FileInputStream
FileInputStream fis = new FileInputString(“c:\\demo.bmp”);
是否需要提高效率:是
BufferedInputStream bis = new BufferedInputStream (fis);
综合:BufferedInputStream bis = new BufferedInputStream (new FileInputStream(“c:\\demo.bmp”));
目的:硬盘上的图片文件,非纯文本:FileOutputStream
FileOutputStream fos = new FileOutputStream(“d:\\demo.bmp”);
是否需要提高效率:是
综合:BufferedOutputStream bos = new BufferedOutputStream(new OutputStream(“d:\\demo_copy.bmp”));
---------------------- android培训、java培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima