3

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

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
aherlambang
  • 14,290
  • 50
  • 150
  • 253

3 Answers3

5

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.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
3

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
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
1

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

Favonius
  • 13,959
  • 3
  • 55
  • 95