缓冲流:
在读写文件的各种流中,最令人烦恼的就是效率问题,
而缓冲流的目的就是提高读写效率
字节输出缓冲流:
package demo; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; //提高写入效率 public class BufferedOutputStreamDemo { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("d:\buffer.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos); bos.write(66); byte[] bytes = "HelloWorld".getBytes(); bos.write(bytes); bos.close(); } }
字节输入缓冲流:
package demo; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; public class BufferedInputStreamDemo { public static void main(String[] args) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\buffer.txt")); byte[] bytes = new byte[10]; int len = 0; while ((len = bis.read(bytes)) != -1) { System.out.print(new String(bytes, 0, len)); } bis.close(); } }
可以利用缓冲流复制文件,和以前的方法做对比:
并且比较下复制时间
package demo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Copy { public static void main(String[] args) throws IOException { long s = System.currentTimeMillis(); copy1(new File("d:\LOL.exe"), new File("e:\LOL.exe")); long e = System.currentTimeMillis(); System.out.println(e - s);// 复制了14154毫秒(14秒) copy2(new File("d:\LOL.exe"), new File("e:\LOL.exe")); // 同样的方法测试时间:129毫秒(0.1秒) copy2(new File("d:\LOL.exe"), new File("e:\LOL.exe")); // 测试时间:94毫秒(不到0.1秒) } public static void copy1(File src, File desc) throws IOException { FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(desc); int len = 0; while ((len = fis.read()) != -1) { fos.write(len); } fos.close(); fis.close(); } public static void copy2(File src, File desc) throws IOException { FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(desc); int len = 0; byte[] bytes = new byte[1024]; while ((len = fis.read(bytes)) != -1) { fos.write(bytes, 0, len); } fos.close(); fis.close(); } public static void copy3(File src, File desc) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc)); int len = 0; byte[] bytes = new byte[1024 * 10]; while ((len = bis.read(bytes)) != -1) { bos.write(bytes, 0, len); } bos.close(); bis.close(); } }
字符缓冲输出流:
package demo; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriterDemo{ public static void main(String[] args) throws IOException { write(); } public static void write() throws IOException{ FileWriter fw = new FileWriter("d:\buffer.txt"); BufferedWriter bfw1 = new BufferedWriter(fw); bfw1.write(100); bfw1.flush(); bfw1.write("你好".toCharArray()); bfw1.newLine();//特有换行方法 //可以用 换行,不过建议使用这种方法,具有平台无关性 bfw1.flush(); bfw1.write("HelloWorld"); bfw1.flush(); bfw1.close(); } }
字符缓冲输入流:
package demo; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedReaderDemo { public static void main(String[] args) throws IOException { read(); } public static void read() throws IOException { int LineNumber = 0; BufferedReader bfr1 = new BufferedReader(new FileReader("d:\read.txt")); // 缓冲流特有方法,读取文本单行 String line = null; while ((line = bfr1.readLine()) != null) { LineNumber++; System.out.println("第" + LineNumber + "行的内容:" + line); } bfr1.close(); } }
字符缓冲流复制文本文件:
package demo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Copy { public static void main(String[] args) throws IOException { BufferedReader bfr1 = new BufferedReader(new FileReader("d:\read.txt")); BufferedWriter bfw1 = new BufferedWriter(new FileWriter("e:\read.txt")); String line = null; while ((line = bfr1.readLine()) != null) { bfw1.write(line); bfw1.newLine(); bfw1.flush(); } bfw1.close(); bfr1.close(); } }
关于各种流的操作规律和选用:
1.明确是要读取还是写入(源和目的)
2.明确是要操作什么类型的,字节还是文本?
3.明确数据所在的设备,在硬盘中还是内存中,或者是网络?(这里还没有介绍内存流和socket)
4.是否需要编码转换,需要利用缓冲流、数组提高效率码?