为什么System.out.println的输出与bufferedwriter不同?

时间:2014-05-09 17:13:37

标签: java bufferedwriter

我正在尝试捕获目录列表,包括子目录和文件。我想把内容写到文本文件中。我遇到的问题是System.out.println()和bufferedwriter()的输出不同。

我的代码在这里


public class testDir2 {

    static int spc_count = -1;
    static void getDirectoryList(File aFile) {
        try {
            File file = new File("D:/Documents/Code - NetBeans/web1/web/Includes/ProdSpecs.txt");
            String content = "xxx";
            // if file exists, Delete and recreate
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            // Define the writer
            FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
            BufferedWriter bw = new BufferedWriter(fw);            
            spc_count++;
            String spcs = "";
            for (int i = 0; i < spc_count; i++) {
                spcs += " ";
            }
            if (aFile.isFile()) {
                System.out.println(spcs + "[FILE] " + aFile.getName());
                bw.write(spcs + "[FILE] " + aFile.getName());
                bw.newLine();
            } else if (aFile.isDirectory()) {
                bw.write(spcs + "[DIR] " + aFile.getName());
                bw.newLine();
                System.out.println(spcs + "[DIR] " + aFile.getName());
                File[] listOfFiles = aFile.listFiles();
                if (listOfFiles != null) {
                    for (int i = 0; i < listOfFiles.length; i++) {
                        getDirectoryList(listOfFiles[i]);
                    }
                } else {
                    System.out.println(spcs + " [ACCESS DENIED]");
                }
            }
            spc_count--;
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String nam = "Web/Documents/Product Specs/";
        File aFile = new File(nam);
        getDirectoryList(aFile);
    }
}

System.out.println()的输出正确如下:


*run:
[DIR] Product Specs
 [DIR] Year 2013
  [DIR] CC & CNC
   [FILE] CNC-1.txt
   [FILE] CNC-2.txt
   [FILE] CNC-3.txt
  [DIR] DCN
   [FILE] DCN-1.txt
   [FILE] DCN-2.txt
   [FILE] DCN-3.txt
   [FILE] DCN-4.txt
  [DIR] TST
   [FILE] TST-1.txt
   [FILE] TST-2.txt
   [FILE] TST-3.txt
BUILD SUCCESSFUL (total time: 0 seconds)*

文本文件的输出不正确,目录列在底部


   [FILE] CNC-1.txt
   [FILE] CNC-2.txt
   [FILE] CNC-3.txt
  [DIR] CC & CNC
   [FILE] DCN-1.txt
   [FILE] DCN-2.txt
   [FILE] DCN-3.txt
   [FILE] DCN-4.txt
  [DIR] DCN
   [FILE] TST-1.txt
   [FILE] TST-2.txt
   [FILE] TST-3.txt
  [DIR] TST
 [DIR] Year 2013
[DIR] Product Specs

非常感谢任何帮助。我已经在这几天了,无法理解。谢谢!

1 个答案:

答案 0 :(得分:2)

每次递归调用方法时,您都会创建一个新的BufferedWriter。我很惊讶他们的工作,但这肯定很难说出输出可能是什么。

我建议您更改代码以使&#34;外部&#34;创建编写器的方法,然后调用&#34; inner&#34;方法,传入作者。当内部方法递归时,它应该通过同一个编写器。所以:

// Generally better to let the exception bubble up than just catch it...
// Adjust according to taste, of course.
static void getDirectoryList(File aFile) throws IOException {
    File file = new File(...);
    try (BufferedWriter writer =
             Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
        getDirectoryList(aFile, writer);
    }
}

private static void getDirectoryList(File aFile, BufferedWriter writer) {
    // Use the writer here, and when you recurse, pass the same one on:
    ...
    getDirectoryList(someOtherFile, writer);
    ...
}
相关问题