Reading text from text files in Java

Sparkz0629

Expert Member
Joined
Jan 7, 2010
Messages
1,579
Reaction score
114
Location
JHBBBB
Right, so i know my title isn't exactly correct, but i don't know how to phrase my problem shortly.
But here's basically it.

I need to read from a file that has a naming convention like so

ABC_DEF_GHI_12-10-2010.txt.

I am using a BufferedReader aswell as a file reader.

The problem is that my code needs to be able to read all the files that have the above mentioned name, excluding the date (There will only be one file that meets this criteria in the fiolder), so it needs to read from:

ABC_DEF_GHI_*.txt.

How would i put this in code, because i tried like this:

BufferedReader in = new BufferedReader(new FileReader("C:/ABC_DEF_GHI_*.txt"));

But this then throws the follwoing error:

The filename, directory name, or volume label syntax is incorrect

Is there some way for me to do this?

Thanks... and sorry if the explanation was a bit dodge...
 
I would actually start with a directory filter: Then search for all files that start with some expression; get the file handle and process accordingly ...

i.e start with something like http://www.exampledepot.com/egs/java.io/GetFiles.html

File dir = new File("directoryName");

// It is also possible to filter the list of returned files.
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("ABC_DEF_GHI");
}
};
children = dir.list(filter);

Now you don't have to worry about the rest and can just process the file ...
 
Last edited:
Unless you need something ultra lightweight (in which case why are you using java?) I recommend Apache Camel.

Code:
from("file:path/to/directory")
.process(new Processor() {
//magic goes here
});
 
Top
Sign up to the MyBroadband newsletter
X