javaSE FileInputStream, FileOutputStream 实现文件复制


Demo.java:

package cn.xxx.copy;

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

public class Demo {
	public static void main(String[] args) {
		long s = System.currentTimeMillis();
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try{
			fis = new FileInputStream("c:\\t.zip");  // 读取流对象
			fos = new FileOutputStream("d:\\t.zip");  // 写入流对象
			//定义字节数组,缓冲
			byte[] bytes = new byte[1024*10];
			//读取数据,写入数组
			int len = 0 ; 
			while((len = fis.read(bytes))!=-1){  // 读取
				fos.write(bytes, 0, len);  // 写入
			}
		}catch(IOException ex){
			System.out.println(ex);
			throw new RuntimeException("文件复制失败");
		}finally{
			try{
				if(fos!=null)
					fos.close();
			}catch(IOException ex){
				throw new RuntimeException("释放资源失败");
			}finally{
				try{
					if(fis!=null)
						fis.close();
				}catch(IOException ex){
					throw new RuntimeException("释放资源失败");
				}
			}
		}
		long e = System.currentTimeMillis();
		System.out.println(e-s);
	}
}


猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/80703879