【问题标题】:Reading multiple txt files from multiple folders in Java从Java中的多个文件夹中读取多个txt文件
【发布时间】:2018-08-26 08:03:31
【问题描述】:

这是我电脑中的数据结构。

amdar(folder) / 20141125(folder) / 300++ txt files
              / 20141126(folder) / 300++ txt files
              / 20141127(folder) / 300++ txt files ...
              / 20141128(folder) / 300++ txt files ...
              / 20141129(folder) / 300++ txt files ...
             ... daily folders are added on and on ...   `

我想用 Java 读取每个文件夹以及 300++ txt 文件。为此,我使用了两个循环:一个用于文件夹,另一个用于文件夹的 txt 文件。文件夹的第一个 for 循环运行良好。 txt 文件的第二个嵌套 for 循环出错了。虽然文件夹从 20141125 开始,但读取的 txt 文件从 20141128 文件夹中的某个位置开始;文件夹中有这么多txt文件:20141125~20141128没有被读取。请看下面的截图。

[Screenshot: Why are the txt files from the 20141125 folder not shown first and even not shown at all?]

为了解决这个问题,我尝试使用while和separate方法而不是嵌套for循环,但是结果(错误),不是从开始文件夹开始读取,是一样的。根据下面的代码,我找不到导致此问题发生的任何原因。你能解释一下为什么吗?谢谢。

    public static void main(String[] args) {
        File dir = new File("c:\\html_test\\amdar");
        File[] folder = dir.listFiles();
            for(File table : folder) {
                System.out.println(table);
                File[] filenames = table.listFiles();
                    for (File file : filenames) {
                        System.out.println(file);
                    }
            }
    }

【问题讨论】:

  • 谢谢!我修改了代码。我成功地创建了表格,读取了一个 txt 文件。但问题仅在我尝试读取其中的多个文件夹和多个 txt 文件时才开始。我觉得这是 Java 的内部错误,因为我尝试了几种方法。
  • 更好,但仍然:阅读minimal reproducible example 并清楚地描述您希望您的代码做什么以及实际发生的情况。我还建议您删除与您的特定问题无关的任何内容。最好提出 10 个不同的明确问题,而不是一个涉及 5 或 10 个不同方面的整体任务!
  • 再次,好多了!我现在很忙,但我希望很快会有一个好的答案。
  • 感谢您的帮助!您的指示让我的问题更加清晰明了!

标签: java file text-files


【解决方案1】:
Right click on the console > Preferences > Console buffer size.
> Uncheck the "Limit console output" checkbox.

我的代码没有错误。

只是结果被80000个字符的限制截断了。

我释放了限制,得到了我期望的结果。

【讨论】:

    【解决方案2】:
    Code written for to read multiple test files from multiple folder: 
    In the try block the FileReader reads a file and the second works as a constructor parameter and has a readline() method.
    
    
        public static void main(String[] args) {
                String dir = "c:\\html_test\\amdar";
            File dir = new File(dir);
                File[] folder = dir.listFiles();
    
            for(File table : folder) {
            if(table.isFile())
            {  
                BufferedReader inputStream = null;      
            try{
                inputStream = new BufferedReader(new FileReader(f));
                String line;
                while ((line = inputStream.readLine()) != null) 
                 {
                   System.out.println(line);
                  }
    
                }  
                 finally {
                            if (inputStream != null) {
                            inputStream.close();
                            }           
    
                         }
            }
                  }
        }
    

    【讨论】:

    • 请不要随便丢码。总是给它一点解释。
    • @GhostCat 为我的回答添加了一些解释!
    • 请对资源使用 try 而不是手动 finally 块。那是 2010 年前的事了。
    • @Varcha Nadar 谢谢你,但是当我将你的代码复制到我的 Eclipse 中时,由于很多红线我无法执行。你能复习一下吗?而且,只有在解决了我遇到的错误之后,我才会使用 BufferedReader,它没有显示 txt 文件的所有标题。我还没有准备好做BufferedReader,因为我什至还不能调用txt文件的所有标题。
    猜你喜欢
    • 1970-01-01
    • 2017-04-21
    • 2020-03-31
    • 2012-01-13
    • 2018-02-23
    • 2015-03-29
    • 1970-01-01
    • 2015-07-19
    • 2021-11-26
    相关资源
    最近更新 更多