通过流进行文件复制
package r.tset;
import java.io.*;
/**
* Date:2023/11/16
* Author:Jo
* Description:文件复制
*/
public class CopyFile {
public static void main(String[] args) {
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream("/Users/**/Desktop/test/mytxt.txt");
fos = new FileOutputStream("/Users/**/Desktop/test/copymytxt.txt");
byte[] bytes = new byte[1024];
int len;
while((len = fis.read(bytes)) != -1){
fos.write(bytes,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
fos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
运行结果
复制成功!