【问题标题】:Java - multiple input files [closed]Java - 多个输入文件[关闭]
【发布时间】:2013-03-17 11:50:26
【问题描述】:

如何在 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();   
   }

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

【问题讨论】:

  • 你试过什么?您阅读一份文档的代码在哪里?您将使用相同的方法,但只需遍历您需要阅读的文档...
  • 如果没有更多上下文,您的问题几乎无法回答。你被困在哪里了?你试过什么?
  • 阿里,你知道怎么读一个文件。您可以使用该知识来读取多个文件。问题出在哪里?
  • Re "Is there any library to read multiple input files? Or I just need to loop and read all files? All files are txt." -- 你需要循环阅读。
  • 您的编辑只是使我的答案不再适用。最好提及明确编辑的部分,以避免在编辑之前适用的答案没有有效答案被否决!

标签: java


【解决方案1】:

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

AFAIK,不。

您的代码适用于将多个文件读入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();  
      } 
   }

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

【讨论】:

    【解决方案2】:

    您可以使用 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);
      }
    

    如果你想读取目录中的所有文件,你想使用:

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

    【讨论】:

    • 他想读取多个文件,而不仅仅是一个文件。不,我不是为了记录而对你投反对票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2013-01-18
    • 2015-06-18
    相关资源
    最近更新 更多