Question

I have a folder User inside that i have so many files with same extension. I want to find all the user file name with a query that is filter.

In the following code how can i retrieve all the files which are equals to the query filter.

  public Collection<User> findUsers(String filter) {

     List<User> filteredList = new ArrayList<User>();
     try {
        FileInputStream fileInputStream = new FileInputStream( "Path of file" );
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        Object object = objectInputStream.readObject();   

        while (object.equals(filter)) {
           filteredList.add((User)object);
           object = objectInputStream.readObject();
        }
        objectInputStream.close();
      }
      catch(Exception ioEx){
             //..............   
      }
   }
   return filteredList;
}

Thanks for any Help.

Was it helpful?

Solution

Here is small snippet to help you:

File folder = new File("/Users/you/folder/");
File[] listOfFiles = folder.listFiles();
List<String> filteredList = new ArrayList<String>();

for (File file : listOfFiles) {
    // Be aware that folder.listFiles() give list with directories and files
    if (file.isFile()) {
        System.out.println(file.getName());

        // apply your filter. for simplicity I am equating the file names
        // name refers to input variable to method
        if(file.getName().toLowerCase().contains(name.toLowerCase())) { 
            // create user object and add it to list. 
            // Change below line with appropriate constructor params
            filteredList.add(file.getName())
        }
    }
}

OTHER TIPS

Step 1 : get the file names

File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();

Step 2 : store there names in some collection object Step 3 : traverse the collection object and find any filter which you want

now , is your prob creating such filter ??

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top