是否有一个标准的Java库可以处理常见的文件操作,例如移动/复制文件/文件夹?
这是通过java.nio操作执行此操作的方法:
java.nio
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(); // previous code: destination.transferFrom(source, 0, source.size()); // to avoid infinite loops, should be: long count = 0; long size = source.size(); while((count += destination.transferFrom(source, count, size-count))<size); } finally { if(source != null) { source.close(); } if(destination != null) { destination.close(); } } }