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.

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.

ReadingACharacterFileUsingFileReader

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 the next page:

 

 

Comments

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options