1

How can I output only the directories and the pdf files like this

[directory]
_____blabla.pdf
_____fefewfew.pdf
[nextdirectory]
_____afdfsdfsdf.pdf
_____... and so on

?

Hope for some help. Here is my code: (the code doesn't work. just tried something) I only have to change something in the main method. The rest is ok.

import java.io.File;

public class DirPrinter implements DirVisitor{

    private String indent;

    public DirPrinter(){
        indent = "";
    }

    @Override
    public void enter(File dir) {
        System.out.printf("%s[%s]%n", indent, dir.getName());
        indent += "  ";
    }

    @Override
    public void visitFile(File f) {
        System.out.printf("%s%s%n", indent, f.getName());
    }

    @Override
    public void exit(File dir) {
        indent = indent.substring(2);
    }

}
------------------------------------------------------------------------
import java.io.File;

public interface DirVisitor {
    void enter(File dir);
    void visitFile(File f);
    void exit(File dir);
}
------------------------------------------------------------------------
import java.io.File;

public class FileTree {

    public static void traverse(File dir, DirVisitor v){
        if(!dir.isDirectory()){
            throw new IllegalArgumentException("No directory!");
        }
    }
}
------------------------------------------------------------------------
import java.io.File;
import java.io.FileFilter;

public class PdfFilter implements FileFilter{

    @Override
    public boolean accept(File pathname) {
        return (pathname.getName().toLowerCase().endsWith(".pdf") ||
                pathname.isDirectory());
    }
}
------------------------------------------------------------------------
import java.io.File;

public class Main {
    public static void main(String[] args){
        PdfFilter pdf = new PdfFilter();
        File directory = new File("C:/Users/Baum/Documents");
//      System.out.println(pdf.accept(directory));

        FileTree ft = new FileTree();
        DirVisitor d = new DirPrinter();

        String[] everythingInThisDir = directory.list();
        for (String name : everythingInThisDir) {
            System.out.println(name);
        }



        ft.traverse(directory, d);
        d.enter(directory);
        d.visitFile(directory);
        d.exit(directory);
    }
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.