NIO.2

jdk1.7中,java对 NIO 极大的扩展,主要增强的是对文件处理 和 文件系统特性的支持

关于其中一些API的使用

 public class TestNIO_2_Path_File {

     //自动资源管理:自动关闭实现 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("1.jpg");
// 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 path2 = Paths.get("E:/jucAndnioCode/NIO/1.jpg"); 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:/jucAndnioCode/NIO/1.jpg");
// Files.createFile(file); Files.deleteIfExists(file);
} @Test
public void test3() throws IOException{
Path path1 = Paths.get("1.jpg");
Path path2 = Paths.get("4.jpg"); 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));
}
}
}

6.NIO2-Path、Paths、Files的更多相关文章

  1. Java NIO学习(Path接口、Paths和Files工具类的使用)

    NIO学习:Paths和Files工具类的使用 JDK1.7引入了新的IO操作类.在java.nio.file包下,Java NIO Path接口和Files类. Path接口:Path表示的是一个目 ...

  2. Java的Path、Paths和Files

    前言 因为这几天被java.nio的这几个接口和工具类卡到了,就顺便地查了一波文档以及使用方法,这篇其实更像是API的复制粘贴,只不过我在注释里多写了一些output和注意事项,看不惯API的可以选择 ...

  3. File、Paths和Files类的使用详解

    Paths:通过get()方法返回一个Path对象,Path用于表示文件路径和文件. Files:提供了大量处理文件的方法,例如文件复制.读取.写入,获取文件属性.快捷遍历文件目录等..... Fil ...

  4. java IO流 (九) Path、Paths、Files的使用

    1.NIO的使用说明:>Java NIO (New IO,Non-Blocking IO)是从Java 1.4版本开始引入的一套新的IO API,可以替代标准的Java IO AP.>NI ...

  5. NIO.2中Path、 Paths、Files类的使用

  6. Java基础教程——File类、Paths类、Files类

    File类 File类在java.io包中.io代表input和output,输入和输出. 代表与平台无关的文件和目录. 可以新建.删除.重命名,但不能访问文件内容. File类里的常量: impor ...

  7. NIO2.0之copy、delete和move

    转自:http://www.importnew.com/15884.html Java 7引入了NIO.2,NIO.2是继承自NIO框架,并增加了新的功能(例如:处理软链接和硬链接的功能).这篇帖子包 ...

  8. C#文件与流(FileStream、StreamWriter 、StreamReader 、File、FileInfo、Directory、directoryInfo、Path、Encoding)

    (FileStream.StreamWriter .StreamReader .File.FileInfo.Directory.DirectoryInfo.Path.Encoding)     C#文 ...

  9. 文件夹和文件、Path类、流、序列化

    循环访问目录树 参考: http://msdn.microsoft.com/zh-cn/library/bb513869.aspx 循环访问目录树”的意思是在指定的根文件夹下,访问每个嵌套子目录中任意 ...

随机推荐

  1. linux下使用SVN上传项目

    linux下使用SVN上传项目 摘自:https://blog.csdn.net/puppet_/article/details/78259591 2017年10月17日 13:51:33 puppe ...

  2. iOS 百度地图报私有api的解决方案

    1.Build Settings-->搜索other linker Flags-->将other linker Flags设置为-objc 2.用2.1.1的版本的百度地图 3.换高德地图

  3. 【Leetcode_easy】754. Reach a Number

    problem 754. Reach a Number solution1: class Solution { public: int reachNumber(int target) { target ...

  4. 01.轮播图之一 :scrollView 轮播

    接触的每个项目,都会用到轮播图的部分,轮播图都写了好多次,用过各种各样的方式写: 这篇总结的博客,我将分为几个篇幅写,希望写完这几篇博客之后,我能总结出自己写这个轮播的优缺和不同之处 scrollvi ...

  5. 【c# 学习笔记】显示接口实现方法

    在接口  一张中,使用了隐式的接口实现方式,即在实现代码中没有制定实现那个接口中的CompareTo方法.相应地,自然就有显式的 接口实现方式,它指的是在实现过程中,明确指出实现哪一个接口中的哪一个方 ...

  6. 【VS开发】fatal error C1853: "Debug\sift.pch"预编译头文件来自编译器的早期版本

    fatal error C1853: "Debug\sift.pch"预编译头文件来自编译器的早期版本 <pre id="best-content-12991040 ...

  7. AutoMapper扩展帮助类

    /// <summary> /// AutoMapper扩展帮助类 /// </summary> public static class AutoMapperExtension ...

  8. python break/continue - python基础入门(10)

    在昨天的文章:python while循环 文章结尾,我们留下了一个bug,当条件成立时,程序陷入了死循环,如何解决呢?     为了规避这个问题,今天介绍两个关键词:break和continue. ...

  9. CDH6.2的fair-scheduler.xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><alloc ...

  10. XML基础知识归纳(通俗易懂)

    XML:可扩展标记型语言 随着json等一些技术的普及,似乎xml的路子越来越窄,虽然xml的一些功能被其他的一些技术代替,但是学习xml还是非常有必要,如果用xml存储一些大量数据,还是有一定优势的 ...