List of utility methods to do Path File Time nio
Path | addTimestamp(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; ... |
boolean | awaitLogMessage(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) { ... |
long | fileCreationTime(Path path) Return the file creation in millis BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class); return attributes.creationTime().to(TimeUnit.MILLISECONDS); |
FileTime | getLastAccessTime(Path file) get Last Access Time return (FileTime) Files.getAttribute(file, "lastAccessTime"); |
FileTime | getLastModificationTime(Path path) get Last Modification Time return Files.readAttributes(path, BasicFileAttributes.class).lastModifiedTime(); |
FileTime | getLastModifiedTime(Path path) get Last Modified Time try { return Files.getLastModifiedTime(path); } catch (Exception e) { return null; |
long | getLastModifiedTimeInMillis(Path path) Returns a file's last modified time in milliseconds. return Files.getLastModifiedTime(path).toMillis();
|
FileTime | getMTime(Path path) get M Time try { return Files.getLastModifiedTime(path); } catch (NoSuchFileException e) { return null; |
void | setFileTimes(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"); ... |
void | setTimes(Path path, Date time) set Times FileTime fileTime = FileTime.fromMillis(time.getTime());
BasicFileAttributeView attrs = Files.getFileAttributeView(path, BasicFileAttributeView.class);
attrs.setTimes(fileTime, fileTime, fileTime);
|