public class CopyFile {
public static void main(String[] args) {
// TODO Auto-generated method stub
new CopyFile().copyFile("d:\\JAVA之路.txt", "e:\\JAVA之路.txt");
}
public void copyFile(String sourceFile,String destFile){
File sourFile = new File(sourceFile);
File desFile = new File(destFile);
//创建字节数组用于缓冲读写
byte[] buf = new byte[1024];
int length = 0;//用于记录单次读入的字节数
//分别创建文件输入流和文件输出流
FileInputStream sourfis = null;
FileOutputStream desfos = null;
try {
sourfis = new FileInputStream(sourFile);
desfos = new FileOutputStream(desFile);
while((length = sourfis.read(buf)) != -1)
desfos.write(buf, 0, length);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
desfos.close();
sourfis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
学习笔记 - FileInputStream FileOutputStream文件拷贝
最新推荐文章于 2024-09-06 09:26:04 发布