Creating and Reading Directories
Listing a File System’s Root Directories
ListDir.java
import java.nio.file.*;
public class ListDir {
public static void main(String[] args) {
Iterable<Path> dirs = FileSystems.getDefault().getRootDirectories();
for (Path name : dirs) {
System.err.println(name);
}
}
}
Creating a Directory
CreateDir.java
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
public class CreateDir {
public static void main(String[] args) {
Path file = Paths.get("test-dir");
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
try {
Files.createDirectory(file, attr);
} catch (IOException x) {
System.out.println("I/O exception: " + x);
}
}
}
CreateDirs.java
import java.io.*;
import java.nio.file.*;
public class CreateDirs {
public static void main(String[] args) {
Path paths = Paths.get("foo/bar/test");
try {
Files.createDirectories(paths);
} catch (IOException x) {
System.out.println("I/O exception: " + x);
}
}
}
Creating a Temporary Directory
CreateTempDir.java
create in pwd
import java.io.*;
import java.nio.file.*;
public class CreateTempDir {
public static void main(String[] args) {
Path path = Paths.get(".");
String prefix = null;
try {
Files.createTempDirectory(path, prefix);
} catch (IOException x) {
System.out.println("I/O exception: " + x);
}
}
}
CreateTempDir2.java
create in /tmp/
import java.io.*;
import java.nio.file.*;
public class CreateTempDir2 {
public static void main(String[] args) {
String prefix = "temp";
try {
Files.createTempDirectory(prefix);
} catch (IOException x) {
System.out.println("I/O exception: " + x);
}
}
}
Listing a Directory’s Contents
ListSourceFiles.java
import java.nio.file.*;
import java.io.*;
public class ListSourceFiles {
public static void main(String[] args) {
Path dir = Paths.get("/tmp");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path file: stream) {
System.out.println(file.getFileName());
}
} catch (IOException | DirectoryIteratorException x) {
System.err.println(x);
}
}
}
Filtering a Directory Listing by Using Globbing
FilterDirByGlob.java
import java.nio.file.*;
import java.io.*;
public class FilterDirByGlob {
public static void main(String[] args) {
Path dir = Paths.get(".");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{java,class,jar}")) {
for (Path entry: stream) {
System.out.println(entry.getFileName());
}
} catch (IOException x) {
System.err.println(x);
}
}
}
Writing Your Own Directory Filter
DirectoryFilter.java
use lambda expression
import java.io.*;
import java.nio.file.*;
public class DirectoryFilter {
public static void main(String[] args) {
/*DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
public boolean accept(Path file) throws IOException {
return Files.isDirectory(file);
}
};*/
Path dir = Paths.get(".");
// try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir,p -> Files.isDirectory(p))) {
for (Path entry: stream) {
System.out.println(entry.getFileName());
}
} catch (IOException x) {
System.err.println(x);
}
}
}