1. 程式人生 > >NIO---2

NIO---2

stream 路徑 dir creat extends 元素 getname port exist

package com.atguigu.nio;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributeView;

import org.junit.Test;

public class TestNIO_2 {


//自動資源管理:自動關閉實現 AutoCloseable 接口的資源
@Test
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 對象
*/
@Test
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 指定的文件相關聯的屬性。
*/
@Test
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 指定文件的大小
*/
@Test
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);
}

@Test
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);
}

@Test
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 對象的字符串表示形式
*/
@Test
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());
}

@Test
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));
}
}
}

NIO---2