简体   繁体   中英

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

I have this path /user/shared/name/Documents. Inside that Documents have sub folders and files.

By using Java swing, how to get all the subfolder names only in that given path? then click the export button and generate the text file for the path values.

Example.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)

Returns a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file. 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.

Also, refer to this question: How do I iterate through the files in a directory in Java?

Oracle has nice tutorial on it: Walking the File Tree

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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