[英]How to copy file attributes from one file to another in Java?
How can I copy file attributes (creation time, modification time, etc...) from one file to another in Java, and just the attributes?如何在 Java 中将文件属性(创建时间、修改时间等)从一个文件复制到另一个文件,并且只复制属性? (not the file itself)
(不是文件本身)
Similarly to touch -r file1 file2
, which is what I ended up doing calling it remotely ( Runtime.getRuntime().exec("touch -r file1 file2")
), but in a Java way (and potentially system agnostic).类似于
touch -r file1 file2
,这就是我最终远程调用它的方式( Runtime.getRuntime().exec("touch -r file1 file2")
),但是以 Java 的方式(并且可能与系统无关)。
I've seen a few other threads about copying mp3/mp4 attributes, but in my case I am interested in the file attributes (without actually copying the file, only its attributes).我已经看到了一些关于复制 mp3/mp4 属性的其他线程,但就我而言,我对文件属性感兴趣(没有实际复制文件,只复制它的属性)。
The 'new' (it's over 10 years old at this point) Files
API does have facilities both for common attributes (such as 'last modified', which all filesystems/OSes tend to have), as well as specific attributes (such as posix ones, including owner/group), in a pluggable fashion. “新”(此时已有 10 多年历史)
Files
API 确实具有通用属性(例如所有文件系统/操作系统往往具有的“最后修改”)以及特定属性(例如 posix那些,包括所有者/组),以可插入的方式。
You can at the very least use these APIs to copy over the common attributes, as well as the posix-special ones, and given that you mentioned touch -r
, that's probably all you need.您至少可以使用这些 API 来复制通用属性以及 posix 特殊属性,鉴于您提到了
touch -r
,这可能就是您所需要的。
Files
API . Files
API 。 Check in particular readAttributes
, setAttribute
, set/getPosixFilePermissions
.特别检查
readAttributes
、 setAttribute
、 set/getPosixFilePermissions
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.