Read multiple character based files in a directory

This article takes you through the very simple steps on how to read all files with in a specified directory.

Read multiple character based files in a directory

Reading a single file:

Before going into reading multiple files, we should first understand how to read a single file. Please look at this article with explains this in detail.

How to read from a character based file using File Reader?

 

Listing all files with in a directory:

The java.io.File class is multi-purpose class that can be used to represent a file in the file system or a directory.

The File.isDirectory() can be used to check whether the entity that the File instance represents is a directory or not.

The File.listFiles() is the only method you need to class to list all the files contained within this directory. This method returns an array of Files

 

Reading all the files with in a directory:

  String multipleFilesDirectory = "multipleFilesDirectory";
  File directory = new File(multipleFilesDirectory); 
  File[] allFilesWithinThisDirectory = directory.listFiles();
 
  System("About to read " + allFilesWithinThisDirectory.length + 
  " within the directory " + directory.getAbsolutePath());
  for (File file: allFilesWithinThisDirectory)
  {
  // read the file here
  }
 

For a full working listing of the source and related classes please check Read multiple character based files in a directory - Java Full Code

Feedback or Questions?

We welcome feedback and questions and will try our best to attend to it as quickly as possible!

Please note that you would have to register before you can post in our forums and this is purely to guard us from the spam-bots. Be assured that we do not send spam mails and our website registration only takes minutes.