import java.io.*;
public class CopyController {
public static void copy(String oldFile, String newFile) {
FileInputStream is = null;
FileOutputStream os = null;
try {
File file = new File(oldFile);
is = new FileInputStream(file);
os = new FileOutputStream(newFile);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
os.write(bytes, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
copy("D:/a.txt", "D:/b.txt"); //注意只能复制文件(音频,文件,电影等),如果是文件夹,将会报错,拒绝访问!
}
}
java 复制文件
最新推荐文章于 2023-03-26 23:17:57 发布