1. 程式人生 > >java NIO 常用的檔案操作方法

java NIO 常用的檔案操作方法

//自動資源管理:自動關閉實現 AutoCloseable 介面的資源
public void test8(){
try(FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.WRITE, StandardOpenOption.CREATE)){

ByteBuffer buf = ByteBuffer.allocate(1024);
inChannel.read(buf);

}catch(IOException e){

}
}

/*
Files常用方法:用於操作內容
SeekableByteChannel newByteChannel(Path path, OpenOption…how) : 獲取與指定檔案的連線,how 指定開啟方式。
DirectoryStream newDirectoryStream(Path path) : 開啟 path 指定的目錄
InputStream newInputStream(Path path, OpenOption…how):獲取 InputStream 物件
OutputStream newOutputStream(Path path, OpenOption…how) : 獲取 OutputStream 物件
*/
public void test7() throws IOException{
SeekableByteChannel newByteChannel = Files.newByteChannel(Paths.get("1.jpg"), StandardOpenOption.READ);

DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(Paths.get("e:/"));

for (Path path : newDirectoryStream) {
System.out.println(path);
}
}

/*
Files常用方法:用於判斷
boolean exists(Path path, LinkOption … opts) : 判斷檔案是否存在
boolean isDirectory(Path path, LinkOption … opts) : 判斷是否是目錄
boolean isExecutable(Path path) : 判斷是否是可執行檔案
boolean isHidden(Path path) : 判斷是否是隱藏檔案
boolean isReadable(Path path) : 判斷檔案是否可讀
boolean isWritable(Path path) : 判斷檔案是否可寫
boolean notExists(Path path, LinkOption … opts) : 判斷檔案是否不存在
public static <A extends BasicFileAttributes> A readAttributes(Path path,Class<A> type,LinkOption... options) : 獲取與 path 指定的檔案相關聯的屬性。
*/
public void test6() throws IOException{
Path path = Paths.get("e:/nio/hello7.txt");
// System.out.println(Files.exists(path, LinkOption.NOFOLLOW_LINKS));

BasicFileAttributes readAttributes = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
System.out.println(readAttributes.creationTime());
System.out.println(readAttributes.lastModifiedTime());

DosFileAttributeView fileAttributeView = Files.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);

fileAttributeView.setHidden(false);
}

/*
Files常用方法:
Path copy(Path src, Path dest, CopyOption … how) : 檔案的複製
Path createDirectory(Path path, FileAttribute<?> … attr) : 建立一個目錄
Path createFile(Path path, FileAttribute<?> … arr) : 建立一個檔案
void delete(Path path) : 刪除一個檔案
Path move(Path src, Path dest, CopyOption…how) : 將 src 移動到 dest 位置
long size(Path path) : 返回 path 指定檔案的大小
*/
public void test5() throws IOException{
Path path1 = Paths.get("e:/nio/hello2.txt");
Path path2 = Paths.get("e:/nio/hello7.txt");

System.out.println(Files.size(path2));

// Files.move(path1, path2, StandardCopyOption.ATOMIC_MOVE);
}

public void test4() throws IOException{
Path dir = Paths.get("e:/nio/nio2");
// Files.createDirectory(dir);

Path file = Paths.get("e:/nio/nio2/hello3.txt");
// Files.createFile(file);

Files.deleteIfExists(file);
}

public void test3() throws IOException{
Path path1 = Paths.get("e:/nio/hello.txt");
Path path2 = Paths.get("e:/nio/hello2.txt");

Files.copy(path1, path2, StandardCopyOption.REPLACE_EXISTING);
}

/*
Paths 提供的 get() 方法用來獲取 Path 物件:
Path get(String first, String … more) : 用於將多個字串串連成路徑。
Path 常用方法:
boolean endsWith(String path) : 判斷是否以 path 路徑結束
boolean startsWith(String path) : 判斷是否以 path 路徑開始
boolean isAbsolute() : 判斷是否是絕對路徑
Path getFileName() : 返回與呼叫 Path 物件關聯的檔名
Path getName(int idx) : 返回的指定索引位置 idx 的路徑名稱
int getNameCount() : 返回Path 根目錄後面元素的數量
Path getParent() :返回Path物件包含整個路徑,不包含 Path 物件指定的檔案路徑
Path getRoot() :返回呼叫 Path 物件的根路徑
Path resolve(Path p) :將相對路徑解析為絕對路徑
Path toAbsolutePath() : 作為絕對路徑返回呼叫 Path 物件
String toString() : 返回呼叫 Path 物件的字串表示形式
*/
public void test2(){
Path path = Paths.get("e:/nio/hello.txt");

System.out.println(path.getParent());
System.out.println(path.getRoot());

// Path newPath = path.resolve("e:/hello.txt");
// System.out.println(newPath);

Path path2 = Paths.get("1.jpg");
Path newPath = path2.toAbsolutePath();
System.out.println(newPath);

System.out.println(path.toString());
}
public void test1(){
Path path = Paths.get("e:/", "nio/hello.txt");

System.out.println(path.endsWith("hello.txt"));
System.out.println(path.startsWith("e:/"));

System.out.println(path.isAbsolute());
System.out.println(path.getFileName());

for (int i = 0; i < path.getNameCount(); i++) {
System.out.println(path.getName(i));
}
}