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:

Recommended Answers

All 4 Replies

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.

You need to handle exceptions throw by these methods(SecurityException).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.