天天看点

IO编程——文件复制操作

将某个文件复制到指定目录。

package com.file;

import java.io.*;

public class test4 {

	
	public static void main(String[] args) {
		FileInputStream fis = null;
		FileOutputStream fos =null;
		
		try {
			fis = new FileInputStream("d:\\qlg.jpg");
			fos = new FileOutputStream("e:\\qlg_复件.jpg");
			byte []buf = new byte[1024]; 
			int n = 0;
			while ((n=fis.read(buf)) != -1){
				fos.write(buf);	//输出到指定文件
			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}finally{	//关闭流
			try {
				fis.close();	
				fos.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}

	}

}