如何使用java代码复制文件呢?
下文笔者讲述java代码复制文件的最简介的方法分享,如下所示
复制文件的实现思路
借助FileInputStream和FileOutputStream 可实现文件复制的效果例:文件快捷复制的示例
public static void copyFile(File sourceFile, File destFile) throws IOException {
if(!destfile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


