java 缓冲流 解决复制文件慢问题

  • Post author:
  • Post category:java




不使用缓冲流

package com.test.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestCopyFile {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		File file=new File("/Users/yangjunbo/kaifa/Test/1.png");
		
		File toFile=new File("/Users/yangjunbo/kaifa/Test/2.png");
		
		long begin=System.currentTimeMillis();
		
		
		try {
			//创建一个字节输入流 用来读取原文件的内容
			FileInputStream fileInputStream=new FileInputStream(file);
			
			//创建一个字节输出流 用来写入复制后的文件
			FileOutputStream fileOutputStream=new FileOutputStream(toFile);
			
			int a;
			//从输入流读
			while((a=fileInputStream.read())!=-1)
			{
				//通过输出流写出去
				fileOutputStream.write(a);
			}
			
			fileInputStream.close();
			
			fileOutputStream.close();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		long end=System.currentTimeMillis();
		
		System.out.println("复制总用时:"+(end-begin)+"毫秒");
		

	}
}

在这里插入图片描述



使用缓冲流

package com.test.file;

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 TestCopyFile2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//用缓冲流复制文件
	    File file=new File("/Users/yangjunbo/kaifa/Test/1.png");
		
		File toFile=new File("/Users/yangjunbo/kaifa/Test/2.png");
		
		long begin=System.currentTimeMillis();
		
		//创建缓冲字节输入流 (一定要先有字节输入流)
		FileInputStream fileInputStream;
		try {
			
			fileInputStream = new FileInputStream(file);
			BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream);
			
			//创建缓冲字节输出流
			FileOutputStream fileOutputStream=new FileOutputStream(toFile);
			BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fileOutputStream);
			
			//从输入流中读取数据 从输出流写出去
			int a;
			while((a=bufferedInputStream.read())!=-1)
			{
				 bufferedOutputStream.write(a);
			}
			
			bufferedInputStream.close();
			
			bufferedOutputStream.close();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		long end=System.currentTimeMillis();
		
		System.out.println("复制文件总用时:"+(end-begin)+"毫秒");
		
		
	}

}

在这里插入图片描述



字符缓冲流

package com.wowowo.io3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestCopyFile3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		File file=new File("d:/aa/person.java");
		
		File toFile=new File("d:/aa/person2.java");
		
		long begin=System.currentTimeMillis();
		try {
			//创建缓冲字符输入流(先创建一个字符输入流)
			FileReader fileReader =new FileReader(file);
			BufferedReader bufferedReader=new BufferedReader(fileReader);
			
			//创建缓冲字符输出流(先创建一个字符输出流)
			FileWriter fileWriter=new FileWriter(toFile);
			BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
			
			//循环从一个地方读出 写到另一个文件
			String str;
			while((str=bufferedReader.readLine())!=null)
				 bufferedWriter.write(str);
			
			bufferedReader.close();
			
			bufferedWriter.close();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		long end=System.currentTimeMillis();
		System.out.println("使用缓冲字符流复制文件总用时:"+(end-begin)+"毫秒");
		

	}

}



版权声明:本文为Rockandrollman原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。