2

如何使用 Java FileChannel 复制文件和目录的保留时间戳?看起来文件在复制到另一个位置时没有保留时间戳。在 Java 中使用 FileChannel 怎么可能?

4

2 回答 2

2

这不是 的作用FileChannel。AFileChannel只是一个字节通道的包装器。

您想要的是为此使用“新的”Java 7 文件 API。如果您想在保留属性的同时将文件复制到某个位置,您可以这样做:

Files.copy(src, dst, StandardCopyOption.COPY_ATTRIBUTES);
于 2014-03-24T17:20:12.460 回答
1

你不能这样做FileChannel,你可以使用 apache commons io:

IOUtils.copy(new FileInputStream(file), new FileOutputStream(file2));
// copy file and preserve the time stamp. the sourceFile and destFile are of type java.io.File
FileUtils.copyFile(sourceFile,destFile);

参考: http: //www.studytrails.com/java-io/file-copying-and-moving-deleting.jsp

于 2014-03-24T17:20:15.850 回答