java输入输出流——文件拷贝

没有优化前的文件拷贝

package com.insigma.chapter13;

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

public class FileCopyDemo {
	/**
	 * 文件拷贝
	 * @param args
	 */
	public static void main(String[] args) {
		long start = System. currentTimeMillis();
		//文件源路径
		String sourcePath="F:/me/2013-09-04_132030.png";
		//要拷贝到的路径
		String targetPath="F:/copy.png";
		
		File ifile=new File(sourcePath);
		File ofile=new File(targetPath);
		
		FileInputStream fileInputStream=null;
		FileOutputStream fileOutputStream=null;
		
		try {
			fileInputStream=new FileInputStream(ifile);
			fileOutputStream=new FileOutputStream(ofile);
			
			//读取文件
			int rel=-1;
			while((rel=fileInputStream.read())!=-1){
				fileOutputStream.write(rel);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				fileInputStream.close();
				fileOutputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		long end = System. currentTimeMillis();
		long time=end-start;
		System.out.println(time);
	}
}

使用装饰流BufferedInputStream来进行优化, BufferedInputStream 时,会创建一个内部缓冲区数组。

package com.insigma.chapter13;

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;
import java.io.InputStream;

public class FileCopyDemo {
	/**
	 * 文件拷贝
	 * @param args
	 */
	public static void main(String[] args) {
		long start = System. currentTimeMillis();
		//文件源路径
		String sourcePath="F:/me/2013-09-04_132030.png";
		//要拷贝到的路径
		String targetPath="F:/copy.png";
		
		File ifile=new File(sourcePath);
		File ofile=new File(targetPath);
		
		FileInputStream fileInputStream=null;
		FileOutputStream fileOutputStream=null;
		
		//
		BufferedInputStream bi=null;
		BufferedOutputStream bo=null;
		
		try {
			fileInputStream=new FileInputStream(ifile);
			fileOutputStream=new FileOutputStream(ofile);
			
			bi=new BufferedInputStream(fileInputStream);
			bo=new BufferedOutputStream(fileOutputStream);
			
			
			//读取文件
			int rel=-1;
			while((rel=bi.read())!=-1){
				bo.write(rel);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				fileInputStream.close();
				fileOutputStream.close();<pre name="code" class="java"><span style="white-space:pre">				</span>bi.close();bo.close();
} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}long end = System. currentTimeMillis();long time=end-start;System.out.println(time);}}

 前两者都是 一个字节一个字节的读取的,接下来一块一块的读取来进行优化 

package com.insigma.chapter13;

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;
import java.io.InputStream;

public class FileCopyDemo {
	/**
	 * 文件拷贝
	 * @param args
	 */
	public static void main(String[] args) {
		long start = System. currentTimeMillis();
		//文件源路径
		String sourcePath="F:/me/2013-09-04_132030.png";
		//要拷贝到的路径
		String targetPath="F:/copy.png";
		
		File ifile=new File(sourcePath);
		File ofile=new File(targetPath);
		
		FileInputStream fileInputStream=null;
		FileOutputStream fileOutputStream=null;
		
		//
		BufferedInputStream bi=null;
		BufferedOutputStream bo=null;
		
		try {
			fileInputStream=new FileInputStream(ifile);
			fileOutputStream=new FileOutputStream(ofile);
			
			bi=new BufferedInputStream(fileInputStream);
			bo=new BufferedOutputStream(fileOutputStream);
			
			
			//读取文件
		/*	int rel=-1;
			while((rel=bi.read())!=-1){
				bo.write(rel);
			}*/
			//要防止文件大小不是1024的倍数而被读坏
			<span style="color:#ff0000;">byte[] temp=new byte[1024];
			int length = 0;
			while((length=bi.read(temp))!=-1){
				bo.write(temp);
			}</span>
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				fileInputStream.close();
				fileOutputStream.close();bi.close();bo.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		long end = System. currentTimeMillis();
		long time=end-start;
		System.out.println(time);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值