【问题标题】:Simulate touch command with Java用Java模拟触摸命令
【发布时间】:2009-09-10 16:56:11
【问题描述】:

我想更改二进制文件的修改时间戳。这样做的最佳方法是什么?

打开和关闭文件是一个不错的选择吗? (我需要一个解决方案,在每个平台和 JVM 上更改时间戳的修改)。

【问题讨论】:

  • 有人应该将此作为增强请求提交给 unix4j:github.com/tools4j/unix4j
  • 我不明白这里的标题和问题之间的关系?
  • @Lealo 见Unix touch command
  • 标题令人困惑,接受的答案也是如此。如果文件不存在,触摸创建一个文件。标题应该改了。

标签: java file touch


【解决方案1】:

File 类有一个setLastModified 方法。这就是 ANT 所做的。

【讨论】:

  • 除了存在已知的 Android 错误,并且 File.setLastModified 在大多数 Android 设备上没有任何作用。
  • 除了 shell touch 创建文件,这不会。
【解决方案2】:

我的 2 美分,基于 @Joe.M answer

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);
}

【讨论】:

    【解决方案3】:

    由于Filea bad abstraction,最好使用FilesPath

    public static void touch(final Path path) throws IOException {
        Objects.requireNonNull(path, "path is null");
        if (Files.exists(path)) {
            Files.setLastModifiedTime(path, FileTime.from(Instant.now()));
        } else {
            Files.createFile(path);
        }
    }
    

    【讨论】:

      【解决方案4】:

      这是一个简单的 sn-p:

      void touch(File file, long timestamp)
      {
          try
          {
              if (!file.exists())
                  new FileOutputStream(file).close();
              file.setLastModified(timestamp);
          }
          catch (IOException e)
          {
          }
      }
      

      【讨论】:

      • 为什么不用file.createNewFile() 而不是new FileOutputStream(file).close()
      【解决方案5】:

      我知道 Apache Ant 有一个 Task 可以做到这一点。
      请参阅source code of Touch(可以向您展示他们是如何做到的)

      他们使用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实现...

      基本上就是调用File.setLastModified(modTime)

      【讨论】:

        【解决方案6】:

        这个问题只提到更新时间戳,但我想我还是把它放在这里。我一直在寻找像 Unix 中那样的触摸,如果它不存在,它也会创建一个文件。

        对于使用 Apache Commons 的任何人,FileUtils.touch(File file) 可以做到这一点。

        这是来自(内联openInputStream(File f))的source

        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);
            }
        }
        

        【讨论】:

        • file.exists 和 file.setLastModified 调用之间存在竞争条件。 IE。此代码将其倒退:首先您继续尝试,然后进行事后诊断。
        【解决方案7】:

        如果您已经在使用Guava

        com.google.common.io.Files.touch(file)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多