gpt4 book ai didi

Java - 复制 JPG 同时保留所有文件属性

转载 作者:行者123 更新时间:2023-12-02 21:01:20 25 4
gpt4 key购买 nike

在保持文件所有属性(创建日期、日期等)的同时移动/复制文件的最佳方法是什么?

谢谢

最佳答案

如果您使用的是 Java 7,请使用 java.nio.file.Files.copy(Path source, Path target, CopyOption... options)

使用 COPY_ATTRIBUTES 选项来维护上次修改时间:

COPY_ATTRIBUTES Attempts to copy the file attributes associated with this file to the target file. The exact file attributes that are copied is platform and file system dependent and therefore unspecified. Minimally, the last-modified-time is copied to the target file if supported by both the source and target file store. Copying of file timestamps may result in precision loss.

已添加强调

对于Java 6及更早版本,Apache commons具有复制文件的功能

    org.apache.commons.io.FileUtils.copyFile(File srcFile, File destFile,
boolean preserveFileDate)

注意评论

Setting preserveFileDate to true tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.

这只会尝试保留修改日期,而不是其他文件属性。

否则,您将必须使用 Runtime.exec 或类似的东西来运行外部进程。

关于Java - 复制 JPG 同时保留所有文件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7643511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com