package study;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
public class FileCopy {
public static void main(String[] args) throws UnsupportedEncodingException{
try {
copyFile("C:\\Users\\Wll\\Desktop\\yy.txt", "C:\\Users\\Wll\\Desktop\\copy.txt");
System.out.println("复制成功!");
} catch (IOException e) {
System.out.println("复制失败!");
System.out.println(e.getMessage());
}
}
/**
* 将 source 路径对应的文件复制到 dest 路径对应的文件
* @param source 源文件
* @param dest 目标文件
* @throws IOException
*/
public static void copyFile(String source, String dest) throws IOException {
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(dest);
byte[] buff = new byte[1024]; // 对每次读入的数据进行缓存
int len = 0; // 每次读取数据的长度
// 边读取边复制
while ((len = in.read(buff)) != -1) {
// read() 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。在某些输入可用之前,此方法将阻塞。
out.write(buff, 0, len);
}
in.close();
out.close();
}
}
文件复制实现
最新推荐文章于 2019-11-19 09:45:57 发布