aherlambang
aherlambang

Reputation: 14418

read whole file in a folder

Is there a way in Java where I can specify a directory in java and it reads the whole file one by one?

Otherwise is there a way to do a regex file read in java? So if all the files in the folder all starts with gh001_12312 gh002_12312, gh003_12911, gh004_22222, gh005_xxxxx, etc

Upvotes: 3

Views: 1174

Answers (3)

Favonius
Favonius

Reputation: 13974

I am reusing the the code from @Tim Bender here:

First get a list of all the needed files as shown by @Tim Bender (shown here again for completeness). And no third party libraries are needed.

File theDirectory = new File("/home/example");
File[] children = theDirectory.listFiles(new FileFilter(){
    public boolean accept(File file) {
        if (file.isFile()) {
           //Check other conditions
           return true;
        }
        return false;
    }
});

Now iterate over this array and use java.nio API for file reading in a single go (without br.readLine())

public StringBuilder readReplaceFile(File f) throws Exception
{
    FileInputStream fis = new FileInputStream(f);
    FileChannel fc = fis.getChannel();

    int sz = (int)fc.size();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);

    CharBuffer cb = decoder.decode(bb);

    StringBuilder outBuffer = new StringBuilder(cb);
    fc.close();
    return outBuffer;
}

Hope this help

Upvotes: 1

Tim Bender
Tim Bender

Reputation: 20442

The standard Java library provides a way of obtaining an array of File elements which are in a directory via File#listFiles. Basically:

File theDirectory = new File("/home/example");
File[] children = theDirectory.listFiles();

Furthermore, there is an overloaded method that allows a filter to be specified which can be used to prune the elements returned in the list.

File theDirectory = new File("/home/example");
File[] children = theDirectory.listFiles(new FileFilter(){
    public boolean accept(File file) {
        if (file.isFile()) {
           //Check other conditions
           return true;
        }
        return false;
    }
});

If you want to do some filtering based on file name as well then have a look at String, Pattern, and Matcher. If you know there will be only files or files will follow a certain naming convention there is also a File.listFiles(FilenameFilter) options which provides only a String representing the file name.

Upvotes: 5

Aravind Yarram
Aravind Yarram

Reputation: 80166

You can use the combination of below methods from commons-io. The first method gives you option of iterating through all the files in a directory, recursively, that match a particular extension (there is another overloaded method that allows you to provide your own filter). The second method reads the entire contents of the file as a String object.

Iterator<File> iterateFiles(File directory,
                                          String[] extensions,
                                          boolean recursive)
String readFileToString(File file)
                               throws IOException

Upvotes: 3

Related Questions