Java Reference
In-Depth Information
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.io.IOException;
public class TryPath {
public static void main(String[] args) {
FileSystem fileSystem = FileSystems.getDefault();
Path path = fileSystem.getPath("garbage.java");
checkPath(path);
path = Paths.get(System.getProperty("user.dir"));
checkPath(path);
// Amend the following path to your environment
path = fileSystem.getPath("D:", "Beginning Java SE 7", "Projects",
"TryPath", "src",
"TryPath.java");
checkPath(path);
return;
}
// Check the attributes of a path
static void checkPath(Path path) {
System.out.println("\n" + path + " has " + path.getNameCount() +
"
elements.");
if(path.isAbsolute()) {
System.out.println(path + " is an absolute path.");
System.out.println("The parent path is " + path.getParent());
System.out.println("The root is " + path.getRoot());
}
else{
System.out.println(path + " is a relative path.");
path = path.toAbsolutePath();
}
if(Files.notExists(path)) {
System.out.println(path + " does not exist.");
return;
}
try {
BasicFileAttributes attr = Files.readAttributes(
Search WWH ::




Custom Search