8

I followed this question:

Now in my case i have 720 files named in this way: "dom 24 mar 2013_00.50.35_128.txt", every file has a different date and time. In testing phase i used Scanner, with a specific txt file, to do some operations on it:

Scanner s = new Scanner(new File("stuff.txt"));

My question is:

How can i reuse scanner and read all 720 files without having to set the precise name on scanner?

Thanks

3
  • see here stackoverflow.com/questions/189094/how-to-scan-a-folder-in-java Commented Jul 18, 2013 at 16:38
  • Can you just read all the files in the directory, or you have more files that you don't want to be read with different naming schemes in the directory as well? Commented Jul 18, 2013 at 16:38
  • 1
    You can't. A new scanner would be created for each file. Commented Jul 18, 2013 at 16:38

4 Answers 4

16

Assuming you have all the files in one place:

File dir = new File("path/to/files/");

for (File file : dir.listFiles()) {
    Scanner s = new Scanner(file);
    ...
    s.close();
}

Note that if you have any files that you don't want to include, you can give listFiles() a FileFilter argument to filter them out.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, all the files are in the same folder.
Damn, you just beat me to it :) +1 for beating me to the answer
how can we read the multiple files in case the files are located in different directories ?
8

Yes, create your file object by pointing it to a directory and then list the files of that directory.

File dir = new File("Dir/ToYour/Files");

if(dir.isDir()) {
   for(File file : dir.listFiles()) {
      if(file.isFile()) {
         //do stuff on a file
      }
   }
} else {
   //do stuff on a file
}

Comments

3

You can try this in this way

 File folder = new File("D:\\DestFile");
 File[] listOfFiles = folder.listFiles();

 for (File file : listOfFiles) {
 if (file.isFile()&&(file.getName().substring(file.getName().lastIndexOf('.')+1).equals("txt"))) {
   // Scanner 
  }
 }

Comments

1
    File file = new File(folderNameFromWhereToRead);

    if(file!=null && file.exists()){
        File[] listOfFiles = file.listFiles();

        if(listOfFiles!=null){

            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                      // DO work
                }
            }
        }
    }

4 Comments

I'm no expert, but I think your if(listOfFiles!=null) is unnecessary. You just created the listOfFiles so how can it be null?
Ughh.. same with your if(file!=null)... you're testing whether an object reference exist which you just made prior to the test!
Null check on lisOfFiles is okay (it could happen if the path exists but is a file) but on the file isn't required. +1 for the isFile() check anyway.
@RaviThapliyal good call, I didn't think about that with listOfFiles because I would have written my code to first check if file is a directory and then processed listFiles().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.