-3

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
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vinoth Mohan
  • 35
  • 1
  • 7

1 Answers1

-2

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

Community
  • 1
  • 1
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
  • Hi Andrii, its throws errors..i suspu ect because of the import. could you post the full script? – Vinoth Mohan Oct 05 '16 at 10:13
  • @VinothMohan noц it should compile and run successfully if you are using `JDK 1.8+` – Andrii Abramov Oct 05 '16 at 21:44
  • I got the output as per above by using another method. But the things is did not showing proper tree format like folder and subfolders and so on. like i mention as per above Example.txt – Vinoth Mohan Oct 06 '16 at 02:45
  • @VinothMohan we are here not for coding. Why don't you try it by yourself ? – Andrii Abramov Oct 08 '16 at 14:22
  • Thanks for reminding me that this forum is not NOT for coding. But knowledge sharing it will very helpful. I am new to this Java swing and i am trying to solve this by learning Java by my self. – Vinoth Mohan Oct 11 '16 at 13:28