user3621130
user3621130

Reputation: 1

Why is my output from System.out.println differ from bufferedwriter?

I am trying to capture the directory listing, including subdirectories and files. I would like to write out the content to a text file. I am having a problem with differing output from the System.out.println() and bufferedwriter().

My code is here


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

The output from the System.out.println() is correct as follows:


*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)*

The output to the text file is incorrect, with the directory being listed in the bottom


   [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

Any help would be greatly appreciated. I have been at this for a few days now and can't figure it out. Thank you!

Upvotes: 0

Views: 191

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500365

You're creating a new BufferedWriter each time you recursively call the method. I'm surprised that's working at all, but it certainly makes it hard to tell what the output is likely to be.

I suggest you change the code to have an "outer" method which creates the writer, and then calls the "inner" method, passing in the writer. When the inner method recurses, it should pass the same writer. So:

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

Upvotes: 2

Related Questions