Add and use PathUtils.setLastModifiedTime(Path) for better precision.
Add and use PathUtils.setLastModifiedTime(Path, Path) for better
precision.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -175,6 +175,10 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add FileUtils.current().
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add and use PathUtils.setLastModifiedTime(Path) for better precision.
+ Add and use PathUtils.setLastModifiedTime(Path, Path) for better precision.
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Update FileEntry to use FileTime instead of long for file time stamps.

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -2821,15 +2821,20 @@ private static File requireFileIfExists(final File file, final String name) {
* Sets the given {@code targetFile}'s last modified date to the value from {@code sourceFile}.
*
* @param sourceFile The source file to query.
- * @param targetFile The target file to set.
+ * @param targetFile The target file or directory to set.
* @throws NullPointerException if sourceFile is {@code null}.
* @throws NullPointerException if targetFile is {@code null}.
* @throws IOException if setting the last-modified time failed.
*/
private static void setLastModified(final File sourceFile, final File targetFile) throws IOException {
Objects.requireNonNull(sourceFile, "sourceFile");
+ Objects.requireNonNull(targetFile, "targetFile");
+ if (targetFile.isFile()) {
+ PathUtils.setLastModifiedTime(targetFile.toPath(), sourceFile.toPath());
+ } else {
setLastModified(targetFile, lastModified(sourceFile));
}
+ }
/**
* Sets the given {@code targetFile}'s last modified date to the given value.
@@ -3133,7 +3138,7 @@ public static void touch(final File file) throws IOException {
if (!file.exists()) {
openOutputStream(file).close();
}
- setLastModified(file, System.currentTimeMillis());
+ PathUtils.setLastModifiedTime(file.toPath());
}
/**

MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -942,6 +942,32 @@ public static String readString(final Path path, final Charset charset) throws I
return stream.collect(Collectors.toList());
}
+ /**
+ * Sets the given {@code targetFile}'s last modified time to the value from {@code sourceFile}.
+ *
+ * @param sourceFile The source path to query.
+ * @param targetFile The target path to set.
+ * @throws NullPointerException if sourceFile is {@code null}.
+ * @throws NullPointerException if targetFile is {@code null}.
+ * @throws IOException if setting the last-modified time failed.
+ * @since 2.12.0
+ */
+ public static void setLastModifiedTime(final Path sourceFile, final Path targetFile) throws IOException {
+ Objects.requireNonNull(sourceFile, "sourceFile");
+ Files.setLastModifiedTime(targetFile, Files.getLastModifiedTime(sourceFile));
+ }
+
+ /**
+ * Sets the last modified time of the given file path to now.
+ *
+ * @param path The file path to set.
+ * @throws IOException if an I/O error occurs.
+ * @since 2.12.0
+ */
+ public static void setLastModifiedTime(final Path path) throws IOException {
+ Files.setLastModifiedTime(path, FileTime.from(Instant.now()));
+ }
+
/**
* Sets the given Path to the {@code readOnly} value.
* <p>

Add and use PathUtils.setLastModifiedTime(Path) for better precision.

Add and use PathUtils.setLastModifiedTime(Path, Path) for better
precision.