java利用io实现文件的复制

第一版,效率较低

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

public class Copy {
	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream("");//源文件路径
		FileOutputStream fos = new FileOutputStream("");//目的路径
		int len = 0;
		while ((len = fis.read())!= -1) {
			fos.write(len);
		}
		fos.close();
		fis.close();
	}
}

升级版

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

public class Copy02 {
	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream("c:\\users\\31537\\desktop\\ddos.java");
		FileOutputStream fos = new FileOutputStream("c:\\users\\31537\\desktop\\ddos.txt");
		byte []data = new byte[1024];//可以为1024的整数倍
		int len = 0;
		while ((len = fis.read(data))!= -1) {
			fos.write(data, 0, len);
		}
		fos.close();
		fis.close();
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_46376562/article/details/105391709