Set last modified timestamp of a file using jimfs in Java

2k Views Asked by At

How can I set a last modified date of a file using jimfs? I have smth. like this:

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");

After creating the stuff I then create a directory iterator like:

try (final DirectoryStream<Path> dirStream = Files.newDirectoryStream(rootPath, "*.pdf")) {
 final Iterator<Path> pathIterator = dirStream.iterator();
}

After that I iterate over the files and read the last modified file, which I then return:

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();
}

The problem is that both files "test1.pdf" and "test2.pdf" have lastModified being "0" so I actually can't really test the behavior as the method would always return the first file in the directory. I tried doing:

File file = new File(filePath.toString());
file.setLastModified(1);

but the method returns false.

UDPATE

I just saw that File#getLastModified() uses the default file system. This means that the default local file system will be used to read the time stamp. And this means I am not able to create a temp file using Jimfs, read the last modified and then assert the paths of those files. The one will have jimfs:// as uri scheme and the another will have OS dependent scheme.

2

There are 2 best solutions below

1
On BEST ANSWER

Jimfs uses the Java 7 file API. It doesn't really mix with the old File API, as File objects are always tied to the default file system. So don't use File.

If you have a Path, you should use the java.nio.file.Files class for most operations on it. In this case, you just need to use

Files.setLastModifiedTime(path, FileTime.fromMillis(millis));
0
On

i am newbie in this but here is my point of view if you choose 1 specific FOLDER and you want to extract the last file from it.

     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());
}
}}