Java NIO2 (Path、Paths 与 Files )

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 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 指定的文件相关联的属性。

*/

@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 常用方法:

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!**

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 27
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值