FarSh018
FarSh018

Reputation: 875

How to get the last modified file from a list of files

I have list of text files in a directory carrying user name and phone number.Every time user changes phone number its saved in new file in same directory.Now i'm searching for an user whose entry is present in multiple files.How do i find the name of last modified file..? below is snippet of code that i hav currently come up with.

public static String queryFile() throws IOException{

    File directory = new File("E:\\idm\\users\\output");
    Boolean isUserPresent = false;
    String queryUser = "Mar25-user6";
    ArrayList arr = new ArrayList();

    if(directory.isDirectory())
    {
        File[] fileNames = directory.listFiles();
        for(int i=0;i<fileNames.length;i++)
        {
          BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileNames[i])));
          while((line = reader.readLine()) != null)
          {
            if(line.contains(queryUser))
            {
                arr.add(fileNames[i]);

            }
          }

        }

        /*
         how to check the last modified file from among files present in Arraylist arr.
         */

         if (arr.isEmpty)
         {
         isUserPresent = false;
         return "";
         }
         else
         {
         isUserPresent = true;
        // return name of file if user present
         }   
    }
}

Going through the javadoc i found File.lastModified() function.Is comparing the value returned by this function the only option..?

Upvotes: 1

Views: 3941

Answers (5)

kdabir
kdabir

Reputation: 9868

You can use this code in your else block to return the newest file which contained the username (arr is the list had the names of all the files that contained the username):

Collections.sort(arr, new Comparator<File>() {
    @Override
    public int compare(String file1, String file2) {
      return Long.valueOf(new File(file2).lastModified()).compareTo(
                    new File(file1).lastModified());

    }

});

arr.get(0); //

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136042

I would do it like this

    File lastModified = null;
    for (File file : directory.listFiles()) {
        if (file.isFile()) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(new FileInputStream(file)));
            try {
                for (String line; (line = reader.readLine()) != null;) {
                    if (line.contains(queryUser)) {
                        arr.add(file);
                        if (lastModified == null
                                || file.lastModified() > lastModified
                                        .lastModified()) {
                            lastModified = file;
                        }
                    }
                }
            } finally {
                reader.close();
            }
        }
    }

Upvotes: 0

NilsH
NilsH

Reputation: 13821

To get the last modified timestamp for a File object, you would indeed invoke the lastModified() method. You already have an ArrayList of files, so traversing it to find the last modified field should be trivial. You're also already looping the list of files, so there should't be a need to loop again and find the last modified file. I would also suggest a few other optimizations of your code:

List<File> fileNames = new ArrayList<File>(); // I would rather call this variable "files" though
long latestModified = -1;    
File lastModifiedFile = null;

// ...

for(File file : directory.listFiles()) {
    // Do your magic
    if(line.contains(queryUser)) {
        filesNames.add(file);
        if(file.lastModified() > latestModified) {
            lastModifiedFile = file;
            latestModified = file.lastModified();
        }
    }
}

// After the loop, the latest modified file will be held i the variable `lastModifiedFile`

I'm not sure what the purpose of the ArrayList is, but maybe you can get rid of it all together.

Upvotes: 0

JSS
JSS

Reputation: 2191

Java does not have off the shelf API for this work but it can easily be implemented by putting some logic around dates by using the .lastModified() method or you can also use Apache FileUtils. I would recommend using Apache Utils.

I would go something like this:

  1. Declare a List for files containing data matching your query
  2. Iterate through the Directory and put file matching in the List above
  3. Finally, Iterate through the first List and check the greatest modified date

This is just an idea you can also achieve same by just iterating through the directory list.

Upvotes: 0

dreambit.io dreambitio
dreambit.io dreambitio

Reputation: 1902

Use file.lastModified() for getting last date modified for each file and then compare with each other

Upvotes: 0

Related Questions