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
...
YOU ARE READING
Java Programming
Randomhttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...