递归方法在使用FileWriter时无法正常工作。

huangapple go评论80阅读模式
英文:

Recursive method doesn't work with FileWriter

问题

我有一个方法应该将一个给定目录的结构以树状视图写入文件但是它不会写入子文件夹及其文件所以我尝试添加了递归调用但出于某种原因它不起作用我该如何修复

public void readFiles() {
    File baseDirectory = new File(path);
    if (baseDirectory.isDirectory()) {
        try (FileWriter writer = new FileWriter("D:/info.txt")) {
            for (File file : baseDirectory.listFiles()) {
                if (file.isFile()) {
                    writer.append(" |------ " + file.getName());
                    writer.append("\n");
                } else if (file.isDirectory()) {
                    writer.append(" |+++++ " + file.getName());
                    writer.append("\n");
                    path = file.getPath().replace("\\", "/");
                    readFiles(); // 递归调用在此处
    
                }
            }
            writer.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

主类

public class Runner {
    public static void main(String[] args) {
        TreeViewer treeViewer = new TreeViewer();
        treeViewer.setPath("D:/test");
        treeViewer.readFiles();
    }
}

[输出文件示例: 1]1

英文:

I have a method that should write down in a file a structure of a given directory in a tree view. But it doesn't write down subfolders and their files, so I tried adding a recursive call, but for some reason it doesn't work. How can i fix it?

public void readFiles() {
        File baseDirectory = new File(path);
        if (baseDirectory.isDirectory()) {
            try (FileWriter writer = new FileWriter("D:/info.txt")) {
                for (File file : baseDirectory.listFiles()) {
                        if (file.isFile()) {
                            writer.append(" |------ " + file.getName());
                            writer.append("\n");
                        } else if (file.isDirectory()) {
                            writer.append(" |+++++ " + file.getName());
                            writer.append("\n");
                            path = file.getPath().replace("\\", "/");
                            readFiles(); // recursive call here

                        }
                }
                writer.flush();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

Main class :

public class Runner {
    public static void main(String[] args) {
        TreeViewer treeViewer = new TreeViewer();
        treeViewer.setPath("D:/test");
        treeViewer.readFiles();
    }
}

[Example of the output file: ][1]

答案1

得分: 0

试试这个。

static void tree(File file, String indent, Writer out) throws IOException {
    out.write(indent + file.getName() + System.lineSeparator());
    if (file.isDirectory())
        for (File child : file.listFiles())
            tree(child, indent + "  ", out);
}

public static void main(String[] args) throws IOException {
    try (Writer out = new FileWriter("D:/info.txt")) {
        tree(new File("D:/test"), "", out);
    }
}
英文:

Try this.

static void tree(File file, String indent, Writer out) throws IOException {
    out.write(indent + file.getName() + System.lineSeparator());
    if (file.isDirectory())
        for (File child : file.listFiles())
            tree(child, indent + "  ", out);
}

public static void main(String[] args) throws IOException {
    try (Writer out = new FileWriter("D:/info.txt")) {
        tree(new File("D:/test"), "", out);
    }
}

huangapple
  • 本文由 发表于 2020年9月10日 05:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63819662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定