Java基础之访问文件与目录——测试文件或目录的路径(TryPath)

控制台程序,测试文件或目录的路径。

 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("E:","JavaProject","BeginningJava","Ch9_Directories","TryPath","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(path,BasicFileAttributes.class); if(attr.isDirectory())
System.out.println(path.getFileName() + " is a directory.");
else if(attr.isRegularFile()){
System.out.println(path.getFileName() + " is a file containing " + attr.size() + " bytes.");
}
System.out.println(path + " was created " + attr.creationTime());
System.out.println(path + " was last accessed " + attr.lastAccessTime());
System.out.println(path + " was last modified " + attr.lastModifiedTime());
System.out.println(path + " is " + attr.size() + " bytes.");
}catch(IOException e){
System.err.println(e);
}
}
}
上一篇:Java程序员的发展前景


下一篇:deeplearing4j学习以及踩过的坑