Java中如何进行文件复制呢?
下文笔者讲述java中文件复制的方法分享,如下所示
文件复制的实现思路
定义一个InputStream和OutputStream
然后借助while循环实现文件复制的操作
例:文件复制的工具类
/**
* source 源文件
* destination 目标文件
*/
public static void copyFile(String source, String destination) {
try {
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(destination);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
fis.close();
fos.close();
System.out.println("File copied successfully!!");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


