I/O

第二章:新I/O
一:从一个简单例子理解path类:
1创建一个path
2获取path的相关路径
3移除path的冗余项
4转换一个path
5合并两个path:
Demo
//creat
Path listing = Paths.get("user/bin/zip");
//get param
System.out.println(listing.getFileName);
System.out.println(listing.getNameCount());
System.out.println(listing.getParent());
System.out.println(listing.getRoot());
System.out.println(listing.subpath(0,2));
System.out.println();
//toRealPath()  get real path
System.out.println(listing.toRealPath());
//resolve(path) let two change one
Path prefix = Paths.get("/uat/");
Path completPath = prefix.resolve("conf/app.properties");
//relativize(path) get between two path
String logging = args[0];
String configuration = args[1];
Path logDir = Paths.get(logging);
Path confDir = Paths.get(configuration);
//equals(path) startWith(path)..endWith(path)  and more


二:java7 path和File的转换
//java7 add two method for change path and file
File file = new File(""../Listing_2_1.java);
Path listing = file.toPath();
listing.toAbsolutePath();
file= listing.toFile();
三:在一个目录中查找文件,以及在目录树中执行相同的文件
//get file in path and do something

Files.walkFileTree(startingDir,new FindJavaVisitor());


private static class FindJavaVisitor extends SimpleFileVisitor<Path>{
@Override
public FileVisitResult visitFile(Path file,BasicFileAttributes attrs){
if(file.toString().endsWith(".java")){
System.out.println(file.getFileName);
}
}
}

猜你喜欢

转载自kelly-xiao.iteye.com/blog/2212642
I/O