如何使用jimfs设置文件的最后修改日期?我有鼻涕。就像这样:
final FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
Path rootPath = Files.createDirectories(fileSystem.getPath("root/path/to/directory"));
Path filePath = rootPath.resolve("test1.pdf");
Path anotherFilePath = rootPath.resolve("test2.pdf");在创建完这些内容之后,我创建了一个目录迭代器,如下所示:
try (final DirectoryStream<Path> dirStream = Files.newDirectoryStream(rootPath, "*.pdf")) {
final Iterator<Path> pathIterator = dirStream.iterator();
}之后,我遍历这些文件并读取最后修改的文件,然后返回:
Path resolveLastModified(Iterator<Path> dirStreamIterator){
long lastModified = Long.MIN_VALUE;
File lastModifiedFile = null;
while (dirStreamIterator.hasNext()) {
File file = new File(dirStreamIterator.next().toString());
final long actualLastModified = file.lastModified();
if (actualLastModified > lastModified) {
lastModifiedFile = file;
lastModified = actualLastModified;
}
}
return lastModifiedFile.toPath();
}问题是,这两个文件"test1.pdf“和"test2.pdf”的lastModified都是"0“,所以我实际上不能真正测试这种行为,因为方法总是返回目录中的第一个文件。我试着做:
File file = new File(filePath.toString());
file.setLastModified(1);但是该方法返回false。
UDPATE
我刚刚看到File#getLastModified()使用默认的文件系统。这意味着默认的本地文件系统将用于读取时间戳。这意味着我无法使用Jimfs创建临时文件,读取最后修改的文件,然后断言这些文件的路径。其中一个将具有jimfs:// as uri方案,另一个将具有依赖于OS的方案。
发布于 2016-02-11 17:19:00
Jimfs使用Java 7文件API。它并不真正与旧的File API混合,因为File对象总是绑定到默认的文件系统。所以不要使用File。
如果您有一个Path,那么对于它上的大多数操作,您都应该使用java.nio.file.Files类。在这种情况下,您只需要使用
Files.setLastModifiedTime(path, FileTime.fromMillis(millis));发布于 2017-08-04 23:28:30
我是新手,但这是我的观点,如果你选择一个特定的文件夹,你想从其中提取最后一个文件。
public static void main(String args[]) {
//choose a FOLDER
File folderX = new File("/home/andy/Downloads");
//extract all de files from that FOLDER
File[] all_files_from_folderX = folderX.listFiles();
System.out.println("all_files_from_folderXDirectories = " +
Arrays.toString(all_files_from_folderX));
//we gonna need a new file
File a_simple_new_file = new File("");
// set to 0L (1JAN1970)
a_simple_new_file.setLastModified(0L);
//check 1 by 1 if is bigger or no
for (File temp : all_files_from_folderX) {
if (temp.lastModified() > a_simple_new_file.lastModified()) {
a_simple_new_file = temp;
}
//at the end the newest will be printed
System.out.println("a_simple_new_file = "+a_simple_new_file.getPath());
}
}} https://stackoverflow.com/questions/35333498
复制相似问题