import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main{
public static void main(String[] args) {
File f = new File("word.txt");
//缓冲输出流的作用:提高了运行效率
FileOutputStream out = null ;
BufferedOutputStream bo =null;
try {
out = new FileOutputStream(f);
bo = new BufferedOutputStream(out);//包装文件输出流
String s = "你好baby";
byte b[] = s.getBytes();
// out.write(b);
bo.write(b);
//使用缓冲字节输出流时,更多的进行刷新操作
bo.flush();//刷新。强制将缓冲区取的数据写入文件中,即使缓冲区没有写满
out.close();
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//缓冲输入流的作用:大大的提高了运行效率
// File f = new File("D:\\JAVA中文介绍\\JAVA中文介绍.CHM");
// long start = System.currentTimeMillis();//获取流开始时的毫秒值
// FileInputStream in = null;
// BufferedInputStream bi = null;//缓冲字节流的效果:大大的提高了运行效率
//
// try {
// in = new FileInputStream(f);
// bi = new BufferedInputStream(in);//将文件字节流包装成缓冲流
// byte by[] = new byte[1024];//缓冲区字节数组(这个缓冲区与Buffered不同)
// while(in.read(by) != -1) {//使用字节流读取数据
//
// }
while(bi.read(by) != -1) {//使用缓冲流读取数据
//
}
// long end = System.currentTimeMillis();//获取流结束时毫秒值
// System.out.println("运行的时间 : " + (end-start));
//
// } catch (FileNotFoundException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// } catch (IOException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// } finally {
// if(in != null) {
// try {
// in.close();
// } catch (IOException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }
// }
// }
}
}
第二部分的结果: