0

I'm trying to print the whole directories tree to .txt file and I'm having some issues since my program is just printing the first file line only.

The code is:

package n1exercici3;

import java.io.*;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

public class DirectorisAFitxer implements FileVisitor<Path> {

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        try {
            PrintWriter writer = new PrintWriter("directoris.txt");
            writer.append("D: "+dir+"\n");
            writer.close();
            return FileVisitResult.CONTINUE;
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        try {

            PrintWriter writer = new PrintWriter("directoris.txt");
            writer.append("  F: "+file+"\n");
            writer.close();
            return FileVisitResult.CONTINUE;
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
        System.out.println("Invalid file!");
        return FileVisitResult.TERMINATE;    
    }



    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        return FileVisitResult.CONTINUE;
    }
}

The file is properly created, so I guess the issues are related to the writer. Right now I'm trying with append() because write() method didn't work, but I'm having the very same outcome.

Ketterle
  • 11
  • 2

1 Answers1

0

Since you are creating a new PrintWriter instance each time you visit a file, you are overwriting the contents of the existing file.

There are two ways to solve this:

  1. Create one PrintWriter instance and use that one instance for all your write operations. Only close this instance once your traversal is finished. This is the recommended way for use cases such as yours.

  2. Create PrintWriter instances that actually append what they write to their target files:

PrintWrtier writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream("directoris.txt", true)));
Thomas Behr
  • 761
  • 4
  • 10
  • Terrific! It worked. BTW, I've chosen the second way, but in case I chose the first one, where should I declare and close the PrintWriter? I'm wondering whether declaring a static PrintWriter in DirectorisAFitxer and closing it in the main class would be fine. Thanks! – Ketterle Feb 21 '23 at 11:07
  • For your case, I'd say `writer` should be an instance field of your `DirectorisAFitxer` class. I suggest to add two new methods, say `createWriter` and `closeWriter`, to `DirectorisAFitxer`. Call `createWriter` right before starting your traversal. Call `closeWriter` right after your traversal is finished. Do *not* use a static field for the `PrintWriter`. – Thomas Behr Feb 21 '23 at 14:23