Skip to content

Commit 04ab528

Browse files
author
Gary Gregory
committed
Add and use PathUtils.setLastModifiedTime(Path) for better precision.
Add and use PathUtils.setLastModifiedTime(Path, Path) for better precision.
1 parent ac4517b commit 04ab528

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ The <action> type attribute can be add,update,fix,remove.
175175
<action dev="ggregory" type="add" due-to="Gary Gregory">
176176
Add FileUtils.current().
177177
</action>
178+
<action dev="ggregory" type="add" due-to="Gary Gregory">
179+
Add and use PathUtils.setLastModifiedTime(Path) for better precision.
180+
Add and use PathUtils.setLastModifiedTime(Path, Path) for better precision.
181+
</action>
178182
<!-- UPDATE -->
179183
<action dev="ggregory" type="add" due-to="Gary Gregory">
180184
Update FileEntry to use FileTime instead of long for file time stamps.

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2821,14 +2821,19 @@ private static File requireFileIfExists(final File file, final String name) {
28212821
* Sets the given {@code targetFile}'s last modified date to the value from {@code sourceFile}.
28222822
*
28232823
* @param sourceFile The source file to query.
2824-
* @param targetFile The target file to set.
2824+
* @param targetFile The target file or directory to set.
28252825
* @throws NullPointerException if sourceFile is {@code null}.
28262826
* @throws NullPointerException if targetFile is {@code null}.
28272827
* @throws IOException if setting the last-modified time failed.
28282828
*/
28292829
private static void setLastModified(final File sourceFile, final File targetFile) throws IOException {
28302830
Objects.requireNonNull(sourceFile, "sourceFile");
2831-
setLastModified(targetFile, lastModified(sourceFile));
2831+
Objects.requireNonNull(targetFile, "targetFile");
2832+
if (targetFile.isFile()) {
2833+
PathUtils.setLastModifiedTime(targetFile.toPath(), sourceFile.toPath());
2834+
} else {
2835+
setLastModified(targetFile, lastModified(sourceFile));
2836+
}
28322837
}
28332838

28342839
/**
@@ -3133,7 +3138,7 @@ public static void touch(final File file) throws IOException {
31333138
if (!file.exists()) {
31343139
openOutputStream(file).close();
31353140
}
3136-
setLastModified(file, System.currentTimeMillis());
3141+
PathUtils.setLastModifiedTime(file.toPath());
31373142
}
31383143

31393144
/**

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,32 @@ static List<Path> relativize(final Collection<Path> collection, final Path paren
942942
return stream.collect(Collectors.toList());
943943
}
944944

945+
/**
946+
* Sets the given {@code targetFile}'s last modified time to the value from {@code sourceFile}.
947+
*
948+
* @param sourceFile The source path to query.
949+
* @param targetFile The target path to set.
950+
* @throws NullPointerException if sourceFile is {@code null}.
951+
* @throws NullPointerException if targetFile is {@code null}.
952+
* @throws IOException if setting the last-modified time failed.
953+
* @since 2.12.0
954+
*/
955+
public static void setLastModifiedTime(final Path sourceFile, final Path targetFile) throws IOException {
956+
Objects.requireNonNull(sourceFile, "sourceFile");
957+
Files.setLastModifiedTime(targetFile, Files.getLastModifiedTime(sourceFile));
958+
}
959+
960+
/**
961+
* Sets the last modified time of the given file path to now.
962+
*
963+
* @param path The file path to set.
964+
* @throws IOException if an I/O error occurs.
965+
* @since 2.12.0
966+
*/
967+
public static void setLastModifiedTime(final Path path) throws IOException {
968+
Files.setLastModifiedTime(path, FileTime.from(Instant.now()));
969+
}
970+
945971
/**
946972
* Sets the given Path to the {@code readOnly} value.
947973
* <p>

0 commit comments

Comments
 (0)