package com.zl.jdk7; import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class PathTest1 { public static void main(String[] args) { Path listing = Paths.get("/home/c3t/1.js"); //得到文件名 System.out.println("File name :"+listing.getFileName()); //获得名称元素的数量,就是目录的数量 System.out.println("number of name elements :"+listing.getNameCount()); //获得父目录路径 System.out.println("parent path :"+listing.getParent()); //得到根目录 System.out.println("Root path :"+listing.getRoot()); //得到根目录(0)到期第二个元素(2)之间的子目录 System.out.println("Subpath from root :" + listing.subpath(0, 2)); try { //.vimrc是一个软链接 得到链接的真实文件地址 Path realPath = Paths.get("/home/c3t/.vimrc").toRealPath(); System.out.println("real path:"+realPath.toString()); } catch (final Exception e) { e.printStackTrace(); } //合并目录,合并后 /home/c3t/conf/application Path prefix = Paths.get("/home/c3t/"); Path completePath = prefix.resolve("conf/application"); System.out.println("resolve:"+completePath.toString()); //获得两个路径之间的路径 结果得到 从 /usr目录到/home/c3t/waller的路径 relativeize:../home/c3t/waller String logging ="/usr"; String configuation = "/home/c3t/waller"; Path logdir = Paths.get(logging); Path confDir = Paths.get(configuation); Path pathtoConfDir = logdir.relativize(confDir); System.out.println("relativeize:"+pathtoConfDir.toString()); //NIO.2 PATH和java已有的file类转换 //java.io.File新增了 toPath方法可以把已有的File转化为Path //Path有toFile可以把path转化为File File file = new File("/home/c3t/1.js"); Path lp = file.toPath(); System.out.println("file to Path:"+lp.toAbsolutePath().toString()); file = lp.toFile(); } }
//创建文件
Path target = Paths.get("/home/c3t/jdk7.txt");
//设置文件权限 由于目录权限限制 结果可能是 rw-r--r--
Set<PosixFilePermission> perms= PosixFilePermissions.fromString("rw-rw-rw-");
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
//创建文件
try {
Files.createFile(target,attr);
} catch (IOException e) {
e.printStackTrace();
}
//删除文件
try {
Files.delete(target);
} catch (IOException e) {
e.printStackTrace();
}
自定义权限的删除和添加
Path profile = Paths.get("/home/c3t/1.js");
try {
//获得属性视图
PosixFileAttributes attrs = Files.readAttributes(profile, PosixFileAttributes.class);
//得到权限集合
Set<PosixFilePermission> posixFilePermission = attrs.permissions();
//清除所有权限
posixFilePermission.clear();
//得到文件爱你所有者
String owner = attrs.owner().getName();
//得到权限的字符串形式
String perms = PosixFilePermissions.toString(posixFilePermission);
//加入自定义权限
posixFilePermission.add(PosixFilePermission.OWNER_READ);
posixFilePermission.add(PosixFilePermission.OWNER_WRITE);
posixFilePermission.add(PosixFilePermission.OTHERS_READ);
posixFilePermission.add(PosixFilePermission.OTHERS_WRITE);
//写入权限
Files.setPosixFilePermissions(profile,posixFilePermission);
} catch (IOException e) {
e.printStackTrace();
}
//读文件
@Test
public void test2() {
Path profile = Paths.get("/home/c3t/1.js");
try (BufferedReader reader = Files.newBufferedReader(profile, StandardCharsets.UTF_8)) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//追加写文件
@Test
public void test3() {
Path profile = Paths.get("/home/c3t/1.js");
try (BufferedWriter writer = Files.newBufferedWriter(profile, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
writer.newLine();
writer.write("i am ok");
} catch (IOException e) {
e.printStackTrace();
}
}