用Java模拟触摸命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1406473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simulate touch command with Java
提问by sinuhepop
I want to change modification timestamp of a binary file. What is the best way for doing this?
我想更改二进制文件的修改时间戳。这样做的最佳方法是什么?
Would opening and closing the file be a good option? (I require a solution where the modification of the timestamp will be changed on every platform and JVM).
打开和关闭文件是一个不错的选择吗?(我需要一个解决方案,其中时间戳的修改将在每个平台和 JVM 上更改)。
采纳答案by Yishai
The File class has a setLastModifiedmethod. That is what ANT does.
File 类有一个setLastModified方法。这就是ANT所做的。
回答by VonC
I know Apache Anthas a Taskwhich does just that.
See the source code of Touch(which can show you how they do it)
我知道Apache Ant有一个Task可以做到这一点。
查看Touch的源代码(可以告诉你他们是怎么做的)
They use FILE_UTILS.setFileLastModified(file, modTime);
, which uses ResourceUtils.setLastModified(new FileResource(file), time);
, which uses a org.apache.tools.ant.types.resources.Touchable
, implemented by org.apache.tools.ant.types.resources.FileResource
...
他们使用FILE_UTILS.setFileLastModified(file, modTime);
, 使用ResourceUtils.setLastModified(new FileResource(file), time);
, 使用org.apache.tools.ant.types.resources.Touchable
, 实现org.apache.tools.ant.types.resources.FileResource
...
Basically, it is a call to File.setLastModified(modTime)
.
基本上,它是对File.setLastModified(modTime)
.
回答by Zach-M
Here's a simple snippet:
这是一个简单的片段:
void touch(File file, long timestamp)
{
try
{
if (!file.exists())
new FileOutputStream(file).close();
file.setLastModified(timestamp);
}
catch (IOException e)
{
}
}
回答by Raekye
This question only mentions updating the timestamp, but I thought I'd put this in here anyways. I was looking for touch like in Unix which will also create a file if it doesn't exist.
这个问题只提到更新时间戳,但我想无论如何我都会把它放在这里。我一直在寻找像 Unix 一样的 touch,如果它不存在,它也会创建一个文件。
For anyone using Apache Commons, there's FileUtils.touch(File file)
that does just that.
对于任何使用 Apache Commons 的人来说,就是FileUtils.touch(File file)
这样做的。
Here's the sourcefrom (inlined openInputStream(File f)
):
这是来自(内联)的来源openInputStream(File f)
:
public static void touch(final File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
}
if (file.canWrite() == false) {
throw new IOException("File '" + file + "' cannot be written to");
}
} else {
final File parent = file.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent + "' could not be created");
}
}
final OutputStream out = new FileOutputStream(file);
IOUtils.closeQuietly(out);
}
final boolean success = file.setLastModified(System.currentTimeMillis());
if (!success) {
throw new IOException("Unable to set the last modification time for " + file);
}
}
回答by Aurélien Ooms
My 2 cents, based on @Joe.M answer
我的 2 美分,基于@Joe.M 的回答
public static void touch(File file) throws IOException{
long timestamp = System.currentTimeMillis();
touch(file, timestamp);
}
public static void touch(File file, long timestamp) throws IOException{
if (!file.exists()) {
new FileOutputStream(file).close();
}
file.setLastModified(timestamp);
}
回答by Joe23
回答by sdgfsdh
Since File
is a bad abstraction, it is better to use Files
and Path
:
由于File
是一个糟糕的抽象,最好使用Files
and Path
:
public static void touch(final Path path) throws IOException {
Objects.requireNotNull(path, "path is null");
if (Files.exists(path)) {
Files.setLastModifiedTime(path, FileTime.from(Instant.now()));
} else {
Files.createFile(path);
}
}