Directories

11 0 0
                                    

Get all directories

BoilerPlate code

=====================

class PracticeGetDirectories{

    public static void main(String[] args){

        Iterable<Path> dirs = FileSystems.getDefault().getRootDirectories();

        for(Path name: dirs){

            System.err.println(name);

        } 

    }

}

=====================

Result:

C:\

D:\

Note: Why use System.err rather than System.out? Don't know! For redirection according to stackoverflow.


Creating Directories

1. Files.createDirectory(Path, FileAttribute<T>) - this creates a directory

2. Files.createDirectories(Path, FileAttribute<T>) - it works the same

3. Files.createTempDirectory(String, FileAttribute<T>)

4. Files.createTempDirectory(Path, String, FileAttribute<T>)

5. Files.newDirectoryStream(Path)


=========================


class PracticeGetDirectories{

public static void main(String[] args) throws IOException{

    Path thePath = Paths.get("C:\\folder1\\folder2\\...folderN");

    try(DirectoryStream<Path> dirs = Files.newDirectoryStream(thePath)){

        for(Path name: dirs){

            System.err.println(name);

        }

    }

}

}

========================

Result:

C:\\folder1\\folder2\\...folderN.CONTENT1

C:\\folder1\\folder2\\...folderN.CONTENT2

...



Java ProgrammingWhere stories live. Discover now