jdk7.NIO.2學習筆記之目錄檔案及許可權

c3tc3tc3t發表於2014-04-09
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();
        }
    }

  

相關文章