I need to read files from a folder(Directory will always be the same)
There will at most be 4 files in the dir, but it could be anything from 0-4 files. They will always have the same name.

i.e. In the dir "Folder" there will be(Could be any combination of these, or all of them) 4 files named "one.txt", "two.txt", "three.txt" and "four.txt". I need to read them in.

How can I do this? :confused:

7 Years
Discussion Span
Last Post by peter_budo

I do not know if you have hard-coded path to folder or not, but once you have path tot the folder you can use File class method list() or listFiles() . First returns array of Strings and second returns array of File objects. Use which ever you prefer, reading files then become simple...


Thanks Peter.

Would the following work?

File dir = new File("directory path");

File[] files = dir.listFiles();
for (/* iterate through files */) {
    filehandler(files[x]); // This handles the file, depending on its name

I can't test it myself right now, this isn't my work PC, but I want to plan my work.

Thank you for the advice, by the way


Thank you for the help, peter!

I used a combination of the code you suggested and the code I had in mind - works perfectly.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.