-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.

Caffeinated
  • 11,982
  • 40
  • 122
  • 216
Ali Ismayilov
  • 1,707
  • 3
  • 22
  • 37
  • 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... – Michael Berry Oct 21 '12 at 14:39
  • 1
    Your question is pretty much unanswerable without more context. Where are you stuck? what have you tried? – Hovercraft Full Of Eels Oct 21 '12 at 14:40
  • http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cad=rja&ved=0CDoQFjAC&url=http%3A%2F%2Fwww.roseindia.net%2Ftutorial%2Fjava%2Fio%2FreadMultipleFiles.html&ei=cgmEUMWHGarM2AXP0oD4Aw&usg=AFQjCNFak_um5pUb6t4GCU3NZugCtGOs3w & http://stackoverflow.com/questions/2296685/how-to-read-input-with-multiple-lines-in-java – Caffeinated Oct 21 '12 at 14:41
  • 1
    Ali, you know how to read one file. You can use that knowledge to read multiple files. Where's the problem? – Adam Oct 21 '12 at 14:41
  • Consider posting code where you try to read more than one file, consider telling us where the files are located and how you plan to identify which files need to be read, consider telling us what you plan to do with the information once obtained, consider telling us what kind of files, text.... You still have left lots of things unsaid. – Hovercraft Full Of Eels Oct 21 '12 at 14:45
  • You need to specify what you have tried! – Chetan Kinger Oct 21 '12 at 14:45
  • 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. – Hovercraft Full Of Eels Oct 21 '12 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! – Chetan Kinger Oct 21 '12 at 15:06
  • It's just a good idea to ask a complete question and to not assume that folks can read minds and understand code, assumptions, or concepts not stated or shown. Asking questions here is a skill and you will get better at this with time and effort. In the mean time, please have a look at this helpful link: [Asking the perfect question](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx) – Hovercraft Full Of Eels Oct 21 '12 at 17:00

2 Answers2

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
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);
    }
  }
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73