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

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

Recursive method doesn't work with FileWriter

问题

  1. 我有一个方法应该将一个给定目录的结构以树状视图写入文件但是它不会写入子文件夹及其文件所以我尝试添加了递归调用但出于某种原因它不起作用我该如何修复
  2. public void readFiles() {
  3. File baseDirectory = new File(path);
  4. if (baseDirectory.isDirectory()) {
  5. try (FileWriter writer = new FileWriter("D:/info.txt")) {
  6. for (File file : baseDirectory.listFiles()) {
  7. if (file.isFile()) {
  8. writer.append(" |------ " + file.getName());
  9. writer.append("\n");
  10. } else if (file.isDirectory()) {
  11. writer.append(" |+++++ " + file.getName());
  12. writer.append("\n");
  13. path = file.getPath().replace("\\", "/");
  14. readFiles(); // 递归调用在此处
  15. }
  16. }
  17. writer.flush();
  18. } catch (IOException ex) {
  19. ex.printStackTrace();
  20. }
  21. }
  22. }
  23. 主类
  24. public class Runner {
  25. public static void main(String[] args) {
  26. TreeViewer treeViewer = new TreeViewer();
  27. treeViewer.setPath("D:/test");
  28. treeViewer.readFiles();
  29. }
  30. }

[输出文件示例: 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?

  1. public void readFiles() {
  2. File baseDirectory = new File(path);
  3. if (baseDirectory.isDirectory()) {
  4. try (FileWriter writer = new FileWriter("D:/info.txt")) {
  5. for (File file : baseDirectory.listFiles()) {
  6. if (file.isFile()) {
  7. writer.append(" |------ " + file.getName());
  8. writer.append("\n");
  9. } else if (file.isDirectory()) {
  10. writer.append(" |+++++ " + file.getName());
  11. writer.append("\n");
  12. path = file.getPath().replace("\\", "/");
  13. readFiles(); // recursive call here
  14. }
  15. }
  16. writer.flush();
  17. } catch (IOException ex) {
  18. ex.printStackTrace();
  19. }
  20. }

Main class :

  1. public class Runner {
  2. public static void main(String[] args) {
  3. TreeViewer treeViewer = new TreeViewer();
  4. treeViewer.setPath("D:/test");
  5. treeViewer.readFiles();
  6. }
  7. }

[Example of the output file: ][1]

答案1

得分: 0

试试这个。

  1. static void tree(File file, String indent, Writer out) throws IOException {
  2. out.write(indent + file.getName() + System.lineSeparator());
  3. if (file.isDirectory())
  4. for (File child : file.listFiles())
  5. tree(child, indent + " ", out);
  6. }
  7. public static void main(String[] args) throws IOException {
  8. try (Writer out = new FileWriter("D:/info.txt")) {
  9. tree(new File("D:/test"), "", out);
  10. }
  11. }
英文:

Try this.

  1. static void tree(File file, String indent, Writer out) throws IOException {
  2. out.write(indent + file.getName() + System.lineSeparator());
  3. if (file.isDirectory())
  4. for (File child : file.listFiles())
  5. tree(child, indent + " ", out);
  6. }
  7. public static void main(String[] args) throws IOException {
  8. try (Writer out = new FileWriter("D:/info.txt")) {
  9. tree(new File("D:/test"), "", out);
  10. }
  11. }

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:

确定