Java - 多个输入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12998841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:05:46  来源:igfitidea点击:

Java - multiple input files

java

提问by Ali Ismayilov

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:

如何在 Java 中读取多个输入文件?我需要阅读多个输入(几个文本文档)并创建一个术语文档矩阵。
我可以像这样读取一个文件:

  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.

是否有任何库可以读取多个输入文件?或者我只需要循环读取所有文件?所有文件都是txt.

回答by Stephen C

Is there any library to read multiple input files?

是否有任何库可以读取多个输入文件?

AFAIK, no.

AFAIK,没有。

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

这是您的代码,适用于将多个文件读入strContent缓冲区。

  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 closecall into a finallyblock 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.

请注意,我已将close调用移动到一个finally块中,以便在读取流时出现问题时不会泄漏文件描述符。主要的变化是简单地将您的代码放入一个循环中,并调整几个语句的顺序。

回答by Yogendra Singh

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

例如,您可以使用 File Stream 类在循环中读取您的文件,您可以使用 FileReader/BufferredFileReader 如下:

  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);
    }
  }