Java - 复制JPG,同时保留所有文件属性 [英] Java - copy JPG while preserving all file attributes

查看:578
本文介绍了Java - 复制JPG,同时保留所有文件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



谢谢

解决方案

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



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


COPY_ATTRIBUTES
尝试将与此文件关联的文件属性复制到
目标文件。复制的确切文件属性是platform和
文件系统依赖,因此未指定。最小化,
last-modified-time被复制到目标文件(如果
源和目标文件存储器都支持的话)。复制文件时间戳可能会造成
的精确度损失。


强调已添加



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

  org.apache.commons.io。 FileUtils.copyFile(文件srcFile,文件destFile,
布尔值preserveFileDate)

注意注释



设置preserveFileDate为true尝试使用File.setLastModified(long)保存文件的最后
修改日期/时间,但是它是
不保证操作将成功。如果修改
操作失败,则不提供指示。


这将只尝试保留修改日期,其他文件属性。



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


What is the best way to move/copy file while maintaining all its attributes (Date created, Date, etc)?

Thank you

解决方案

If you are using Java 7, use java.nio.file.Files.copy(Path source, Path target, CopyOption... options)

Use the COPY_ATTRIBUTES option to maintain the last modified time:

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.

Emphasis added

For Java 6 and earlier, Apache commons has a function to copy files

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

Note the comments

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.

This will only try to preserve the modified date, and not other file attributes.

Otherwise, you will have to use Runtime.exec or something similar to run an external process.

这篇关于Java - 复制JPG,同时保留所有文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆