-4

How does one read multiple input files in Java? I need to read multiple inputs (several text documents) and create a Term Document Matrix.
I can read one file like this:

  File file = new File("a.txt"); 
  int ch;
  StringBuffer strContent = new StringBuffer("");
  FileInputStream stream = null;  
  try
   {
     stream = new FileInputStream(file);   
     while( (ch = stream.read()) != -1)
        strContent.append((char)ch); 
      stream.close();   
   }

Is there any library to read multiple input files? Or I just need to loop and read all files? All files are txt.

9
  • 4
    What have you tried? Where is your code for reading one document? You would use the same approach, but simply iterate over the documents you need to read... Commented Oct 21, 2012 at 14:39
  • 1
    Your question is pretty much unanswerable without more context. Where are you stuck? what have you tried? Commented Oct 21, 2012 at 14:40
  • 1
    Ali, you know how to read one file. You can use that knowledge to read multiple files. Where's the problem? Commented Oct 21, 2012 at 14:41
  • 1
    Re "Is there any library to read multiple input files? Or I just need to loop and read all files? All files are txt." -- you need to loop and read. Commented Oct 21, 2012 at 14:58
  • 1
    Your edit just makes my answer not applicable anymore. It would be a good idea to mention what part is edited explicitly to avoid answers that were applicable before the edit to not have valid answers be downvoted! Commented Oct 21, 2012 at 15:06

2 Answers 2

4

Is there any library to read multiple input files?

AFAIK, no.

Here is your code adapted to read multiple files into the strContent buffer.

  String names = new String[]{"a.txt", "b.txt", "c.txt"};
  StringBuffer strContent = new StringBuffer("");

  for (String name : names) {
      File file = new File(name); 
      int ch;
      FileInputStream stream = null;  
      try {
          stream = new FileInputStream(file);   
          while( (ch = stream.read()) != -1) {
              strContent.append((char) ch); 
          }
      } finally {
          stream.close();  
      } 
   }

Note that I've moved the close call into a finally block so that you don't leak file descriptors if there is a problem reading the stream. The main changes are to simply put your code into a loop, and tweak the order of a couple of the statements.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use File Stream classes to read your files in loop for example, you can use FileReader/BufferredFileReader as below:

  String[] fileNames = new String[]{  "fileNameWithPath1", "fileNameWithPath2"...};

  for(String fileName: fileNames ) {  
      BufferredFileReader reader = 
                      new BufferredFileReader(new FileReader(fileName));
      System.ount.println("Start reading file : "+fileName);
      String line = null;
      while((line=reader.nextLine())!= null){
         System.out.println(line);
      }
      reader.close();
      System.ount.println("End reading file : "+fileName);
  }

If you want to read all files in directory, you want to use:

  File directory = new File("directoryName");
  File[] filesInDir = directory.listFiles();//list all files in directory
  for(File file: filesInDir) {  
    if(!file.isDirectory()){ //read the file if not directory
      BufferredFileReader reader = 
                      new BufferredFileReader(new FileReader(file));
      System.ount.println("Start reading file : "+fileName);
      String line = null;
      while((line=reader.nextLine())!= null){
         System.out.println(line);
      }
      reader.close();
      System.ount.println("End reading file : "+fileName);
    }
  }

1 Comment

He wants to read multiple files and not just one file. And no, I didn't down vote you just for the record.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.