public static void main(String[] args) throws IOException {
//bufferedOutput();
//bufferedInput();
//copyFile();
}
/**
* 文件复制操作(操作文件最快)
* @throws IOException
*/
private static void copyFile() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\test.avi"));
byte[] bytes = new byte[1024];
int len = 0;
while((len = bis.read(bytes)) != -1){
bos.write(bytes, 0, len);
}
bos.close();
bis.close();
System.out.println("复制完成");
}
/**
* BufferedInputStream:提升读取效率,执行顺序和BufferedOutputStream一样
*/
private static void bufferedInput() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.txt"));
byte[] b = new byte[1024];
int len = 0;
while((len = bis.read(b)) != -1){
System.out.println(new String(b, 0, len));
}
bis.close();//关闭缓冲流也会吧字节流关闭
}
/**
* BufferedOutputStream:提升写入效率
* 没提升效率前顺序:程序->JVM->计算机底层写入(每次读取*字节,每次都调用一次计算机底层)
* 提升效率后:程序->JVM(先不调用计算机底层,先将数据缓冲,数据读取完毕后->计算机底层)
* OutputStream的子类,使用方法都一样
*/
private static void bufferedOutput() throws IOException{
FileOutputStream fos = new FileOutputStream("D:\\a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);//传入要提升效率的字节输出流
bos.write("HelloWorld".getBytes());
bos.close();
System.out.println("写入完成");
}
Java缓冲字节流BufferedOutputStream(输出),BufferedInputStream(输入),对文件的复制操作
最新推荐文章于 2024-04-27 23:13:56 发布