简体   繁体   English

如何使用Java swing获取文件夹和子文件夹名称并在txt中导出

[英]How to get folder and subfolders name using Java swing and export in txt

I have this path /user/shared/name/Documents.我有这个路径 /user/shared/name/Documents。 Inside that Documents have sub folders and files.在那个 Documents 里面有子文件夹和文件。

By using Java swing, how to get all the subfolder names only in that given path?通过使用 Java swing,如何仅在给定路径中获取所有子文件夹名称? then click the export button and generate the text file for the path values.然后单击导出按钮并为路径值生成文本文件。

Example.txt :示例.txt:

path /Users/User1/Desktop/arc_testing 2/

Directory:/Users/User1/Desktop/arc_testing 2/099923
----Directory:/Users/User1/Desktop/arc_testing 2/099923/000_Fonts
----Directory:/Users/User1/Desktop/arc_testing 2/099923/000_Fonts/fancybox
----Directory:/Users/User1/Desktop/arc_testing 2/099923/010_Correspondence
----Directory:/Users/User1/Desktop/arc_testing 2/099923/010_Correspondence/smart
Directory:/Users/User1/Desktop/arc_testing 2/099924
----Directory:/Users/User1/Desktop/arc_testing 2/099924/000_Fonts
----Directory:/Users/User1/Desktop/arc_testing 2/099924/000_Fonts/fancybox
----Directory:/Users/User1/Desktop/arc_testing 2/099924/010_Correspondence
----Directory:/Users/User1/Desktop/arc_testing 2/099924/010_Correspondence/smart
----Directory:/Users/User1/Desktop/arc_testing 2/099924/010_Correspondence/smart/images
----Directory:/Users/User1/Desktop/arc_testing 2/099924/010_Correspondence/smart/js
----Directory:/Users/User1/Desktop/arc_testing 2/099924/010_Correspondence/smart/services
----Directory:/Users/User1/Desktop/arc_testing 2/099924/010_Correspondence/smart/styles
Directory:/Users/User1/Desktop/arc_testing 2/099925
Directory:/Users/User1/Desktop/arc_testing 2/099926/020_Supplied
----Directory:/Users/User1/Desktop/arc_testing 2/099925/020_Supplied/doc
----Directory:/Users/User1/Desktop/arc_testing 2/099925/020_Supplied/font
----Directory:/Users/User1/Desktop/arc_testing 2/099925/020_Supplied/makefont
----Directory:/Users/User1/Desktop/arc_testing 2/099925/020_Supplied/pdf-ok
----Directory:/Users/User1/Desktop/arc_testing 2/099925/020_Supplied/tutorial

If you are using Java 8, take a look at useful method Stream walk(Path start, FileVisitOption... options)如果您使用的是 Java 8,请查看有用的方法Stream walk(Path start, FileVisitOption... options)

Returns a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file.通过遍历以给定起始文件为根的文件树,返回一个懒惰地填充有 Path 的 Stream。 The file tree is traversed depth-first, the elements in the stream are Path objects that are obtained as if by resolving the relative path against start.文件树是深度优先遍历的,流中的元素是 Path 对象,就像通过解析相对路径对 start 一样获得。

Also, refer to this question: How do I iterate through the files in a directory in Java?另外,请参考这个问题: How do I iterate through the files in a directory in Java?

Oracle has nice tutorial on it: Walking the File Tree Oracle 有很好的教程: 遍历文件树

Here is an example of traversing file system:下面是一个遍历文件系统的例子:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileTreeExample {

    private static final String START_PATH = "/home/xxx/dev/python";

    public static void main(String[] args) throws IOException {
        Files.walk(Paths.get(START_PATH))
            .map(Path::toFile)
            .filter(File::isDirectory)
            .map(File::getAbsolutePath)
            .forEach(System.out::println); // or write it to file

    }

}

Result:结果:

/home/xxx/dev/python /home/xxx/dev/python/mongo_tutorial /home/xxx/dev/python/mongo_tutorial/mongo /home/xxx/dev/python/mongo_tutorial/mongo/ pycache /home/xxx/dev/python/mongo_tutorial/.idea /home/xxx/dev/python/mongo_tutorial/.idea/dictionaries /home/xxx/dev/python/pyminds /home/xxx/dev/python/pyminds/messages /home/xxx/dev/python/pyminds/results /home/xxx/dev/python/pyminds/.idea /home/xxx/dev/python/pyminds/ pycache /home/xxx/dev/python/bforce /home/xxx/dev/python/trash /home/xxx/dev/python /home/xxx/dev/python/mongo_tutorial /home/xxx/dev/python/mongo_tutorial/mongo /home/xxx/dev/python/mongo_tutorial/mongo/ pycache /home/xxx/dev /python/mongo_tutorial/.idea /home/xxx/dev/python/mongo_tutorial/.idea/dictionaries /home/xxx/dev/python/pyminds /home/xxx/dev/python/pyminds/messages /home/xxx/dev /python/pyminds/results /home/xxx/dev/python/pyminds/.idea /home/xxx/dev/python/pyminds/ pycache /home/xxx/dev/python/bforce /home/xxx/dev/python/垃圾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM