1. 程式人生 > >自己寫的java複製任意檔案程式碼

自己寫的java複製任意檔案程式碼

指定test1方法中的file();引數 即可

package File;

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

public class FileOutputStreamtest1 {
	public static void main(String[] args) {
		test1();
	}
	
	static void test1() {
		File file1 = new File("text.txt");
		File file2 = new File("text2.txt");
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(file1);
			fos = new FileOutputStream(file2);
			int len = 0;
			byte[] bs = new byte[10];
			while ((len =fis.read(bs) ) != -1) {
				fos.write(bs, 0, len);
			}
			fis.close();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}