java nio paths_JAVA NIO學習四:Path&Paths&Files 學習

importjava.io.IOException;importjava.nio.ByteBuffer;importjava.nio.channels.FileChannel;importjava.nio.channels.SeekableByteChannel;importjava.nio.file.DirectoryStream;importjava.nio.file.Files;importjava.nio.file.LinkOption;importjava.nio.file.Path;importjava.nio.file.Paths;importjava.nio.file.StandardCopyOption;importjava.nio.file.StandardOpenOption;importjava.nio.file.attribute.BasicFileAttributes;importjava.nio.file.attribute.DosFileAttributeView;importorg.junit.Test;public classTestNIO_2 {//自動資源管理:自動關閉實現 AutoCloseable 接口的資源

@Testpublic voidtest8(){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 對象*/@Testpublic void test7() throwsIOException{

SeekableByteChannel newByteChannel= Files.newByteChannel(Paths.get("1.jpg"), StandardOpenOption.READ);

DirectoryStream 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 readAttributes(Path path,Class type,LinkOption... options) : 獲取與 path 指定的文件相關聯的屬性。*/@Testpublic void test6() throwsIOException{

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 指定文件的大小*/@Testpublic void test5() throwsIOException{

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);

}

@Testpublic void test4() throwsIOException{

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);

}

@Testpublic void test3() throwsIOException{

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 對象的字符串表示形式*/@Testpublic voidtest2(){

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());

}

@Testpublic voidtest1(){

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));

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值