public static void saveFiles(String origPath, String destPath) {
File file = new File(origPath);
System.out.println("开始复制文件");
InputStream in = null;
BufferedOutputStream fos=null;
try {
fos= new BufferedOutputStream(new FileOutputStream(destPath));
if (!file.exists() || file.isDirectory())
throw new FileNotFoundException();
in = new BufferedInputStream(new FileInputStream(file));
byte[] temp = new byte[1024];
int size = 0;
while ((size = in.read(temp)) != -1) {
fos.write(temp, 0, size);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("复制文件完成");
}
Java 文件拷贝
最新推荐文章于 2024-06-14 17:48:16 发布