From 99bd330730e376e5cf377667f9e7560eac0b993e Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 4 Aug 2017 17:38:07 +0200 Subject: [PATCH] Added File.touch --- src/dorkbox/util/FileUtil.java | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/dorkbox/util/FileUtil.java b/src/dorkbox/util/FileUtil.java index d89cbea..7b641c0 100644 --- a/src/dorkbox/util/FileUtil.java +++ b/src/dorkbox/util/FileUtil.java @@ -1287,6 +1287,53 @@ class FileUtil { return null; } + /** + * Touches a file, so that it's timestamp is right now. If the file is not created, it will be created automatically. + * + * @return true if the touch succeeded, false otherwise + */ + public static + boolean touch(String file) { + long timestamp = System.currentTimeMillis(); + return touch(new File(file).getAbsoluteFile(), timestamp); + } + + /** + * Touches a file, so that it's timestamp is right now. If the file is not created, it will be created automatically. + * + * @return true if the touch succeeded, false otherwise + */ + public static + boolean touch(File file) { + long timestamp = System.currentTimeMillis(); + return touch(file, timestamp); + } + + /** + * Touches a file, so that it's timestamp is right now. If the file is not created, it will be created automatically. + * + * @return true if the touch succeeded, false otherwise + */ + public static + boolean touch(File file, long timestamp) { + if (!file.exists()) { + boolean mkdirs = file.getParentFile() + .mkdirs(); + if (!mkdirs) { + // error creating the parent directories. + return false; + } + + try { + new FileOutputStream(file).close(); + } catch (IOException ignored) { + return false; + } + } + + return file.setLastModified(timestamp); + } + //-----------------------------------------------------------------------