Java Utililty Methods Path File Time nio

List of utility methods to do Path File Time nio

Description

The list of methods to do Path File Time nio are organized into topic(s).

Method

PathaddTimestamp(Path base, TimeUnit unit)
add Timestamp
long now = System.currentTimeMillis();
String filename = base.getFileName().toString();
int i = filename.indexOf('.');
String postfix = addTimeStampGetPostFix(new Date(now), unit);
if (i > 0) {
    filename = filename.substring(0, i) + "-" + postfix + filename.substring(i);
} else {
    filename = filename + "-" + postfix;
...
booleanawaitLogMessage(String message, long timeout, Path logFilePath)
Waits for a log message to become present in the specified log file
long start = System.currentTimeMillis();
do {
    try {
        List<String> logLines = Files.readAllLines(logFilePath);
        if (logLines.stream().filter(line -> line.matches(message)).count() > 0) {
            return true;
    } catch (Exception e) {
...
longfileCreationTime(Path path)
Return the file creation in millis
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
return attributes.creationTime().to(TimeUnit.MILLISECONDS);
FileTimegetLastAccessTime(Path file)
get Last Access Time
return (FileTime) Files.getAttribute(file, "lastAccessTime");
FileTimegetLastModificationTime(Path path)
get Last Modification Time
return Files.readAttributes(path, BasicFileAttributes.class).lastModifiedTime();
FileTimegetLastModifiedTime(Path path)
get Last Modified Time
try {
    return Files.getLastModifiedTime(path);
} catch (Exception e) {
return null;
longgetLastModifiedTimeInMillis(Path path)
Returns a file's last modified time in milliseconds.
return Files.getLastModifiedTime(path).toMillis();
FileTimegetMTime(Path path)
get M Time
try {
    return Files.getLastModifiedTime(path);
} catch (NoSuchFileException e) {
    return null;
voidsetFileTimes(String path, Date creationTime, Date lastAccessTime, Date lastModifiedTime)
set File Times
Class<?> pathClass = Class.forName("java.nio.file.Path");
Class<?> fileSystemsClass = Class.forName("java.nio.file.FileSystems");
Class<?> fileSystemClass = Class.forName("java.nio.file.FileSystem");
Class<?> basicFileAttributeViewClass = Class.forName("java.nio.file.attribute.BasicFileAttributeView");
Class<?> filesClass = Class.forName("java.nio.file.Files");
Class<?> linkOptionClass = Class.forName("java.nio.file.LinkOption");
Class<?> fileTimeClass = Class.forName("java.nio.file.attribute.FileTime");
Method fileSystemsGetDefaultMethod = fileSystemsClass.getMethod("getDefault");
...
voidsetTimes(Path path, Date time)
set Times
FileTime fileTime = FileTime.fromMillis(time.getTime());
BasicFileAttributeView attrs = Files.getFileAttributeView(path, BasicFileAttributeView.class);
attrs.setTimes(fileTime, fileTime, fileTime);