Added File.touch

This commit is contained in:
nathan 2017-08-04 17:38:07 +02:00
parent 99b95074ab
commit 99bd330730
1 changed files with 47 additions and 0 deletions

View File

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