NIO-5补充
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-5补充的更多相关文章
- BIO、NIO、AIO入门认识
同步.异步.阻塞.非阻塞概念理解. 同步: 比如在执行某个逻辑业务,在没有得到结果之前一直处于等待阻塞状态,得到结果后才继续执行 异步: 比如在执行某个逻辑业务,在没有得到结果可以去干其他的事情,等待 ...
- Netty快速入门(03)Java NIO 介绍-Buffer
NIO 介绍 NIO,可以说是New IO,也可以说是non-blocking IO,具体怎么解释都可以. NIO 1是在JSR51里面定义的,在JDK1.4中引入,因为BolckingIO不支持高并 ...
- 最美应用-从Android研发project师的角度之[最美时光]
最美应用-从Android研发project师的角度之最美时光 @author ASCE1885的 Github 简书 微博 CSDN 近期发现最美应用这样一个站点.它会定期推介一些非常有意思的app ...
- java.io 包下的类有哪些 + 面试题
java.io 包下的类有哪些 + 面试题 IO 介绍 IO 是 Input/Output 的缩写,它是基于流模型实现的,比如操作文件时使用输入流和输出流来写入和读取文件等. IO 分类 传统的 IO ...
- NIO Q&A(持续补充。。。。)
Q:NIO是非阻塞的.但调用的selector.select()方法会阻塞.这和NIO非阻塞岂不是矛盾了? A:非阻塞指的是 IO 事件本身不阻塞,但是获取 IO 事件的 select 方法是需要阻塞 ...
- Java NIO 学习笔记 读写结合补充
小练习:nio读写文件,将fileread中的内容读取到filewrite中 try { //创建输入通道 FileInputStream fis = new FileInputStream(&quo ...
- Java NIO 学习笔记五 缓冲区补充
1.缓冲区分配 方法 以 ByteBuffer 为例 (1)使用静态方法 ByteBuffer buffer = ByteBuffer.allocate( 500 ); allocate() 方法 ...
- Java NIO 学习笔记 缓冲区补充
1.缓冲区分配 方法 以 ByteBuffer 为例 (1)使用静态方法 ByteBuffer buffer = ByteBuffer.allocate( 500 ); allocate() 方法 ...
- (转)也谈BIO | NIO | AIO (Java版)
原文地址: https://my.oschina.net/bluesky0leon/blog/132361 关于BIO | NIO | AIO的讨论一直存在,有时候也很容易让人混淆,就我的理解,给出一 ...
- 【原创】NIO框架入门(四):Android与MINA2、Netty4的跨平台UDP双向通信实战
概述 本文演示的是一个Android客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo. 当前由于NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能. ...
随机推荐
- 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to tem ...
- windows下php扩展存在但无法加载的问题
1.可能存在多个php环境,扩展没有放对地方 2.扩展和php版本不对应,例如,php是32位,扩展是64位:或者php是nts版本,但是扩展不是nts版本.
- Codeforces 833B The Bakery dp线段树
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- 问题分析 - 电容的ESR
ESR,是Equivalent Series Resistance三个单词的缩写,翻译过来就是“等效串连电阻” 理论上,一个完美的电容,自身不会产生任何能量损失,但是实际上,因为制造电容的材料有电阻, ...
- Hadoop YARN 的工作流程简述
1.Client 向 YARN 提交应用程序,其中包括 ApplicationMaster 程序及启动 ApplicationMaster 命令2.ResourceManager 为该 Applica ...
- (转) 使用vivado创建工程 1
此文全文转自:http://svenand.blogdrive.com/archive/169.html#.WaUV9IiGNPY ,非常感谢! 本人在vivado 2015.4版本测试! When ...
- 远程调试openstack
之前一直没有找到方法调试openstack的horizon代码,现在终于找到方法了,特别感谢下面这篇博客,讲解非常清晰: http://blog.csdn.net/tantexian/article/ ...
- Android图片压缩工具MCompressor
这是一个简单的图片压缩工具(MCompressor),可自定义压缩的格式和质量,以及压缩后存储的文件路径,可决定对多大的文件进行压缩. 使用方法 build.gradle文件 Step 1. Add ...
- js获取本周、上周的开始结束时间
这两天在做一个报表体统,其中涉及到了一个根据本周,上周,本月,上月的时间来进行查询的问题,在这个我就教一下大家怎么实现,大家如果有更好的实现方法的,我也希望大家能说出来,我们交流交流. 首先呢,我写了 ...
- codeblocks 设置代码自动补全
熟悉使用一些开发类IDE的朋友对代码自动补全一定印象深刻,如Visual studio,eclipse等,我们在程序中定义的那一个个超长的变量函数名只需打出几个字母就可自动补全,但是在codebloc ...