Java面试必问通讯框架NIO,原理详解

NIO
流与块
通道与缓冲区
缓冲区状态变量
文件 NIO 实例
选择器
套接字 NIO 实例
内存映射文件
NIO与IO对比
Path
Files
NIO
新的输入/输出 (NIO) 库是在 JDK 1.4 中引入的,弥补了原来的 I/O 的不足,提供了高速的、面向块的 I/O。java

NIO核心组件:编程

通道(Channels)
缓冲区(Buffers)
选择器(Selectors)
流与块
I/O 与 NIO 最重要的区别是数据打包和传输的方式,I/O 以流的方式处理数据,而 NIO 以块的方式处理数据。数组

面向流的 I/O 一次处理一个字节数据:一个输入流产生一个字节数据,一个输出流消费一个字节数据。 为流式数据建立过滤器很是容易,连接几个过滤器,以便每一个过滤器只负责复杂处理机制的一部分。不利的一面是,面向流的 I/O 一般至关慢。服务器

面向块的 I/O 一次处理一个数据块,按块处理数据比按流处理数据要快得多。 可是面向块的 I/O 缺乏一些面向流的 I/O 所具备的优雅性和简单性。网络

I/O 包和 NIO 已经很好地集成了,java.io. 已经以 NIO 为基础从新实现了,因此如今它能够利用 NIO 的一些特性。 例如,java.io. 包中的一些类包含以块的形式读写数据的方法,这使得即便在面向流的系统中,处理速度也会更快。并发

通道与缓冲区app

  1. 通道

通道 Channel 是对原 I/O 包中的流的模拟,能够经过它读取和写入数据。dom

通道与流的不一样之处在于,流只能在一个方向上移动(一个流必须是 InputStream 或者 OutputStream 的子类), 而通道是双向的,能够用于读、写或者同时用于读写。socket

通道包括如下类型:分布式

FileChannel:从文件中读写数据;
DatagramChannel:经过 UDP 读写网络中数据;
SocketChannel:经过 TCP 读写网络中数据;
ServerSocketChannel:能够监听新进来的 TCP 链接,对每个新进来的链接都会建立一个 SocketChannel。

  1. 缓冲区

发送给一个通道的全部数据都必须首先放到缓冲区中,一样地,从通道中读取的任何数据都要先读到缓冲区中。也就是说,不会直接对通道进行读写数据,而是要先通过缓冲区。

缓冲区实质上是一个数组,但它不只仅是一个数组。缓冲区提供了对数据的结构化访问,并且还能够跟踪系统的读/写进程。

缓冲区包括如下类型:

ByteBuffer
CharBuffer
ShortBuffer
IntBuffer
LongBuffer
FloatBuffer
DoubleBuffer
缓冲区状态变量
capacity:最大容量;
position:当前已经读写的字节数;
limit:还能够读写的字节数。
状态变量的改变过程举例:

① 新建一个大小为 8 个字节的缓冲区,此时 position 为 0,而 limit = capacity = 8。capacity 变量不会改变,下面的讨论会忽略它。

② 从输入通道中读取 5 个字节数据写入缓冲区中,此时 position 为 5,limit 保持不变。

③ 在将缓冲区的数据写到输出通道以前,须要先调用 flip() 方法,这个方法将 limit 设置为当前 position,并将 position 设置为 0。

④ 从缓冲区中取 4 个字节到输出缓冲中,此时 position 设为 4。

⑤ 最后须要调用 clear() 方法来清空缓冲区,此时 position 和 limit 都被设置为最初位置。

文件 NIO 实例
FileChannel的使用
开启FileChannel
从FileChannel读取数据/写入数据
3.关闭FileChannel

public class FileChannelDemo {

public static void main(String[] args) throws IOException {
    //1.建立一个RandomAccessFile(随机访问文件)对象经过RandomAccessFile对象的getChannel()方法。
    RandomAccessFile raf=new RandomAccessFile("demo6.txt","rw");
    FileChannel fc=raf.getChannel();

    //使用FileChannel的read()方法读取数据:
    ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
    int bys=fc.read(byteBuffer);

    //使用FileChannel的write()方法写入数据:
    ByteBuffer byteBuffer2=ByteBuffer.allocate(1024);
    byteBuffer2.put("hello".getBytes());
    fc.write(byteBuffer2);
    
    //3.关闭FileChannel
    fc.close();
}

}
如下展现了使用 NIO 快速复制文件的实例:
public class CopyFile {

public static void main(String[] args) throws IOException {
    String srcFile="国旗歌.mp4";
    String destFile="demo3.mp4";
    long start = System.currentTimeMillis();
    //copyFile(srcFile,destFile); //共耗时:75309毫秒
    //copyFile2(srcFile,destFile); //共耗时:153毫秒
    //copyFile3(srcFile,destFile);//共耗时:282毫秒
    //copyFile4(srcFile,destFile);//共耗时:44毫秒
    copyFile5(srcFile,destFile);//共耗时:共耗时:113毫秒
    long end = System.currentTimeMillis();
    System.out.println("共耗时:" + (end - start) + "毫秒");
}

/**
 * 基本字节流一次读写一个字节
 */
public static void copyFile(String srcFile,String destFile) throws IOException {
    FileInputStream fis=new FileInputStream(srcFile);
    FileOutputStream fos=new FileOutputStream(destFile);

    int by=0;
    while((by=fis.read())!=-1){
        fos.write(by);
    }

    fis.close();
    fos.close();
}

/**
 * 基本字节流一次读写一个字节数组
 */
public static void copyFile2(String srcFile,String destFile) throws IOException{
    FileInputStream fis=new FileInputStream(srcFile);
    FileOutputStream fos=new FileOutputStream(destFile);

    int len=0;
    byte[] bys=new byte[1024];
    while((len=fis.read(bys))!=-1){
        fos.write(bys,0,len);
    }

    fis.close();
    fos.close();
}

/**
 * 高效字节流一次读写一个字节
 */
public static void copyFile3(String srcFile,String destFile) throws IOException{
    BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
    BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));

    int by=0;
    while((by=bis.read())!=-1){
        bos.write(by);
    }

    bis.close();
    bos.close();
}

/**
 * 高效字节流一次读写一个字节数组
 */
public static void copyFile4(String srcFile,String destFile) throws IOException{
    BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
    BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));

    int len=0;
    byte[] bys=new byte[1024];
    while((len=bis.read(bys))!=-1){
        bos.write(bys,0,len);
    }

    bis.close();
    bos.close();
}

/**
 * 使用FileChannel复制文件
 */
public static void copyFile5(String srcFile,String destFile) throws IOException{
    FileInputStream fis=new FileInputStream(srcFile);
    //获取输入字节流的文件通道
    FileChannel fcin=fis.getChannel();
    FileOutputStream fos=new FileOutputStream(destFile);
    //获取输出字节流的文件通道
    FileChannel fcout=fos.getChannel();

    //为缓冲区分配 1024 个字节
    ByteBuffer buffer = ByteBuffer.allocateDirect(1024);

    while(true){
         //从输入通道中读取数据到缓冲区中
        int r = fcin.read(buffer);
        // read() 返回 -1 表示 EOF
        if(r==-1){
            break;
        }
        //切换读写
        buffer.flip();
        //把缓冲区的内容写入输出文件中
        fcout.write(buffer);
        //清空缓冲区
        buffer.clear();
    }
}

}
SocketChannel和ServerSocketChannel的使用
SocketChannel用于建立基于TCP协议的客户端对象,由于SocketChannel中不存在accept()方法, 因此,它不能成为一个服务端程序。 经过connect()方法,SocketChannel对象能够链接到其余TCP服务器程序。

ServerSocketChannel容许咱们监听TCP协议请求,经过ServerSocketChannel的accept()方法建立一个SocketChannel对象用户从客户端读/写数据。

服务端:
经过ServerSocketChannel 绑定ip地址和端口号
经过ServerSocketChannel的accept()方法建立一个SocketChannel对象用户从客户端读/写数据
建立读数据/写数据缓冲区对象来读取客户端数据或向客户端发送数据
关闭SocketChannel和ServerSocketChannel
public class Server {

public static void main(String[] args) throws IOException {
    //经过ServerSocketChannel 的open()方法建立一个ServerSocketChannel对象
    ServerSocketChannel ssc=ServerSocketChannel.open();

    //1. 经过ServerSocketChannel 绑定ip地址和端口号
    ssc.socket().bind(new InetSocketAddress(InetAddress.getByName("LAPTOP-D9966H06"),8888));

    //2. 经过ServerSocketChannel的accept()方法建立一个SocketChannel对象用户从客户端读/写数据
    SocketChannel sc=ssc.accept();

    //3. 建立读数据/写数据缓冲区对象来读取客户端数据或向客户端发送数据
    //读取客户端发送的数据
    ByteBuffer buffer=ByteBuffer.allocate(1024);
    //从通道中读取数据到缓冲区
    sc.read(buffer);
    StringBuffer sb=new StringBuffer();
    buffer.flip();
    while(buffer.hasRemaining()){
        sb.append((char)buffer.get());
    }
    System.out.println(sb.toString());

    ByteBuffer buffer2=ByteBuffer.allocate(1024);
    //向客户端发送数据
    buffer2.put("data has been received.".getBytes());
    buffer2.flip();
    sc.write(buffer2);

    //4. 关闭SocketChannel和ServerSocketChannel
    sc.close();
    ssc.close();
}

}
客户端:
1.经过SocketChannel链接到远程服务器

2.建立读数据/写数据缓冲区对象来读取服务端数据或向服务端发送数据

3.关闭SocketChannel

public class Client {

public static void main(String[] args) throws IOException {
    //1.经过SocketChannel链接到远程服务器
    SocketChannel sc=SocketChannel.open();
    sc.connect(new InetSocketAddress(InetAddress.getByName("LAPTOP-D9966H06"),8888));

    //2.建立读数据/写数据缓冲区对象来读取服务端数据或向服务端发送数据
    //向通道中写入数据
    ByteBuffer buffer=ByteBuffer.allocate(1024);
    buffer.put("hello".getBytes());
    buffer.flip();
    sc.write(buffer);

    //读取从客户端中获取的数据
    ByteBuffer buffer2=ByteBuffer.allocate(1024);
    sc.read(buffer2);
    StringBuffer sb=new StringBuffer();
    buffer2.flip();
    while(buffer2.hasRemaining()){
        sb.append((char)buffer2.get());
    }
    System.out.println(sb.toString());

    //3.关闭SocketChannel
    sc.close();
}

}
DatagramChannel的使用
DataGramChannel,相似于java 网络编程的DatagramSocket类; 使用UDP进行网络传输, UDP是无链接,面向数据报文段的协议。

服务端:
public class Server {

public static void main(String[] args) throws IOException {
    DatagramChannel dc= DatagramChannel.open();
    dc.bind(new InetSocketAddress(InetAddress.getByName("LAPTOP-D9966H06"),8888));


    //建立读数据/写数据缓冲区对象来读取客户端数据或向客户端发送数据
    //读取客户端发送的数据
    ByteBuffer buffer=ByteBuffer.allocate(1024);
    //从通道中读取数据到缓冲区
    dc.receive(buffer);
    StringBuffer sb=new StringBuffer();
    buffer.flip();
    while(buffer.hasRemaining()){
        sb.append((char)buffer.get());
    }
    System.out.println(sb.toString());

    ByteBuffer buffer2=ByteBuffer.allocate(1024);
    //向客户端发送数据
    buffer2.put("data has been received.".getBytes());
    buffer2.flip();
    dc.send(buffer2,new InetSocketAddress(InetAddress.getByName("LAPTOP-D9966H06"),9999));
    
    dc.close();
}

}
客户端:
public class Client {

public static void main(String[] args) throws IOException {
    DatagramChannel dc= DatagramChannel.open();
    dc.bind(new InetSocketAddress(InetAddress.getByName("LAPTOP-D9966H06"),9999));

    //建立读数据/写数据缓冲区对象来读取服务端数据或向服务端发送数据
    //向通道中写入数据
    ByteBuffer buffer=ByteBuffer.allocate(1024);
    buffer.put("hello".getBytes());
    buffer.flip();
    dc.send(buffer,new InetSocketAddress(InetAddress.getByName("LAPTOP-D9966H06"),8888));

    //读取从客户端中获取的数据
    ByteBuffer buffer2=ByteBuffer.allocate(1024);
    dc.receive(buffer2);
    StringBuffer sb=new StringBuffer();
    buffer2.flip();
    while(buffer2.hasRemaining()){
        sb.append((char)buffer2.get());
    }
    System.out.println(sb.toString());
    
    dc.close();
}

}
通道之间的数据传输
在Java NIO中若是一个channel是FileChannel类型的,那么他能够直接把数据传输到另外一个channel。

transferFrom() :transferFrom方法把数据从通道源传输到FileChannel
transferTo() :transferTo方法把FileChannel数据传输到另外一个FileChhannel
public static void copyFile6(String srcFile,String destFile) throws IOException {

FileInputStream fis = new FileInputStream(srcFile);
//获取输入字节流的文件通道
FileChannel fcin = fis.getChannel();
FileOutputStream fos = new FileOutputStream(destFile);
//获取输出字节流的文件通道
FileChannel fcout = fos.getChannel();

//fcin通道中读出count bytes ,并写入fcout通道中
//fcin.transferTo(0,fcin.size(),fcout);
//或者
fcout.transferFrom(fcin,0,fcin.size());

}
选择器
NIO 经常被叫作非阻塞 IO,主要是由于 NIO 在网络通讯中的非阻塞特性被普遍使用。

NIO 实现了 IO 多路复用中的 Reactor 模型,一个线程 Thread 使用一个选择器 Selector 经过轮询的方式 去监听多个通道 Channel 上的事件,从而让一个线程就能够处理多个事件。

经过配置监听的通道 Channel 为非阻塞,那么当 Channel 上的 IO 事件还未到达时, 就不会进入阻塞状态一直等待,而是继续轮询其它 Channel,找到 IO 事件已经到达的 Channel 执行。

由于建立和切换线程的开销很大,所以使用一个线程来处理多个事件而不是一个线程处理一个事件, 对于 IO 密集型的应用具备很好地性能。

应该注意的是,只有套接字 Channel 才能配置为非阻塞,而 FileChannel 不能, 为 FileChannel 配置非阻塞也没有意义。

使用Selector的优势:

使用更少的线程来就能够来处理通道了, 相比使用多个线程, 避免了线程上下文切换带来的开销。

  1. 建立选择器

Selector selector = Selector.open();

  1. 将通道注册到选择器上

ServerSocketChannel ssChannel = ServerSocketChannel.open();
ssChannel.configureBlocking(false);//通道必须配置为非阻塞模式
ssChannel.register(selector, SelectionKey.OP_ACCEPT);
通道必须配置为非阻塞模式,不然使用选择器就没有任何意义了,由于若是通道在某个事件上被阻塞,那么服务器就不能响应其它事件,必须等待这个事件处理完毕才能去处理其它事件,显然这和选择器的做用背道而驰。

在将通道注册到选择器上时,还须要指定要注册的具体事件,主要有如下几类:

SelectionKey.OP_CONNECT
SelectionKey.OP_ACCEPT
SelectionKey.OP_READ
SelectionKey.OP_WRITE
它们在 SelectionKey 的定义以下:

public static final int OP_READ = 1 << 0;
public static final int OP_WRITE = 1 << 2;
public static final int OP_CONNECT = 1 << 3;
public static final int OP_ACCEPT = 1 << 4;
能够看出每一个事件能够被当成一个位域,从而组成事件集整数。例如:

int interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;

  1. 监听事件

int num = selector.select();
使用 select() 来监听到达的事件,它会一直阻塞直到有至少一个事件到达。

  1. 获取到达的事件

Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> keyIterator = keys.iterator();
while (keyIterator.hasNext()) {

SelectionKey key = keyIterator.next();
if (key.isAcceptable()) {
    // ...
} else if (key.isReadable()) {
    // ...
}
keyIterator.remove();

}

  1. 事件循环

由于一次 select() 调用不能处理完全部的事件,而且服务器端有可能须要一直监听事件,所以服务器端处理事件的代码通常会放在一个死循环内。

while (true) {

int num = selector.select();
Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> keyIterator = keys.iterator();
while (keyIterator.hasNext()) {
    SelectionKey key = keyIterator.next();
    if (key.isAcceptable()) {
        // ...
    } else if (key.isReadable()) {
        // ...
    }
    keyIterator.remove();
}

}
套接字 NIO 实例
public class NIOServer {

public static void main(String[] args) throws IOException {
    //1. 建立选择器
    Selector selector = Selector.open();

    //2.将通道注册到选择器上
    ServerSocketChannel ssChannel = ServerSocketChannel.open();
    ssChannel.configureBlocking(false);
    //通道必须配置为非阻塞模式,不然使用选择器就没有任何意义了
    ssChannel.register(selector, SelectionKey.OP_ACCEPT);

    ServerSocket ss=ssChannel.socket();
    ss.bind(new InetSocketAddress("127.0.0.1",8888));

    while (true){
        //3. 监听事件
        selector.select();

        //4. 获取到达的事件
        Set<SelectionKey> keys = selector.selectedKeys();
        Iterator<SelectionKey> keyIterator = keys.iterator();
        while (keyIterator.hasNext()) {
            SelectionKey key = keyIterator.next();
            if (key.isAcceptable()) {
                ServerSocketChannel ssChannel1 = (ServerSocketChannel) key.channel();

                // 服务器会为每一个新链接建立一个 SocketChannel
                SocketChannel sChannel = ssChannel1.accept();
                sChannel.configureBlocking(false);

                // 这个新链接主要用于从客户端读取数据
                sChannel.register(selector, SelectionKey.OP_READ);

            } else if (key.isReadable()) {
                SocketChannel sChannel = (SocketChannel) key.channel();
                System.out.println(readDataFromSocketChannel(sChannel));
                sChannel.close();
            }
            keyIterator.remove();
        }
    }
}

private static String readDataFromSocketChannel(SocketChannel sChannel) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    StringBuilder data = new StringBuilder();

    while (true) {
        buffer.clear();
        int r = sChannel.read(buffer);
        if (r == -1) {
            break;
        }
        buffer.flip();
        int limit = buffer.limit();
        char[] dst = new char[limit];
        for (int i = 0; i < limit; i++) {
            dst[i] = (char) buffer.get(i);
        }
        data.append(dst);
        buffer.clear();
    }
    return data.toString();
}

}
public class NIOClient {

public static void main(String[] args) throws IOException {
    Socket socket = new Socket("127.0.0.1", 8888);
    OutputStream out = socket.getOutputStream();
    String s = "hello world";
    out.write(s.getBytes());
    out.close();
}

}
内存映射文件
内存映射文件 I/O 是一种读和写文件数据的方法,它能够比常规的基于流或者基于通道的 I/O 快得多。

向内存映射文件写入多是危险的,只是改变数组的单个元素这样的简单操做,就可能会直接修改磁盘上的文件。修改数据与将数据保存到磁盘是没有分开的。

下面代码行将文件的前 1024 个字节映射到内存中,map() 方法返回一个 MappedByteBuffer,它是 ByteBuffer 的子类。所以,能够像使用其余任何 ByteBuffer 同样使用新映射的缓冲区,操做系统会在须要时负责执行映射。

MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, 1024);
NIO与IO对比
NIO 与普通 I/O 的区别主要有如下三点:

NIO 是非阻塞的;
NIO 面向块,I/O 面向流。
NIO有选择器,而I/O没有。
Path
Java7中文件IO发生了很大的变化,专门引入了不少新的类来取代原来的 基于java.io.File的文件IO操做方式。

建立一个Path
使用Paths工具类的get()方法建立Path对象

public class PathDemo {

public static void main(String[] args) {
    //方式一
    Path path=Paths.get("demo5.txt");
    System.out.println(path);

    //方式二
    Path path2 = FileSystems.getDefault().getPath("demo5.txt");
    System.out.println(path2);
}

}
File和Path之间的转换,File和URI之间的转换
public class PathDemo2 {

public static void main(String[] args) {
    Path path=Paths.get("demo5.txt");
    File file=path.toFile();
    URI uri=path.toUri();
    System.out.println(path);
    System.out.println(file);
    System.out.println(uri);
}

}
demo5.txt
demo5.txt
file:///F:/Java_Review/05Java/JavaIO/demo5.txt
获取Path的相关信息
public class PathDemo3 {

public static void main(String[] args) {
    Path path= Paths.get("demo3\\test3.txt");
    System.out.println("文件名:"+ path.getFileName());
    System.out.println("名称元素的数量:"+path.getNameCount());
    System.out.println("父路径:"+ path.getParent());
    System.out.println("根路径:"+ path.getRoot());
    System.out.println("是不是绝对路径:"+path.isAbsolute());

    //startWith() 参数既能够是字符串,也能够是Path
    System.out.println("是不是以路径demo3开头:"+path.startsWith(Paths.get("demo3")));
    System.out.println("该路径的字符串形式:"+path.toString());
}

}
文件名:test3.txt
名称元素的数量:2
父路径:demo3
根路径:null
是不是绝对路径:false
是不是以路径demo3开头:true
该路径的字符串形式:demo3test3.txt
移除Path中的冗余项
.表示的是当前目录

..表示父目录或者说是上一级目录

normalize() : 返回一个路径,该路径是取出冗余项的路径。

toRealPath() : 能够当作,先进行toAbsolutePath()操做,而后进行normalize()操做

public class PathDemo4 {

public static void main(String[] args) throws IOException {
    Path path= Paths.get("./demo3");

    System.out.println("original :"+ path.toAbsolutePath());
    System.out.println("after normalize:"+ path.toAbsolutePath().normalize());
    System.out.println("after toRealPath:"+ path.toRealPath());
}

}
original :F:Java_Review05JavaJavaIO.demo3
after normalize:F:Java_Review05JavaJavaIOdemo3
after toRealPath:F:Java_Review05JavaJavaIOdemo3
public class PathDemo5 {

public static void main(String[] args) throws IOException {
    Path path= Paths.get("../JavaIO");

    System.out.println("original :"+ path.toAbsolutePath());
    System.out.println("after normalize:"+ path.toAbsolutePath().normalize());
    System.out.println("after toRealPath:"+ path.toRealPath());
}

}
original :F:Java_Review05JavaJavaIO..JavaIO
after normalize:F:Java_Review05JavaJavaIO
after toRealPath:F:Java_Review05JavaJavaIO
Files
java.nio.file.Files类是和java.nio.file.Path相结合使用的

检查给定的Path在文件系统中是否存在
Files.exists():检测文件路径是否存在

public class FilesDemo {

public static void main(String[] args) {
    Path path = Paths.get("demo5.txt");
    //LinkOptions.NOFOLLOW_LINKS:表示检测时不包含符号连接文件。
    boolean isExist= Files.exists(path,new LinkOption[]{LinkOption.NOFOLLOW_LINKS});
    System.out.println(isExist);
}

}
建立文件/文件夹
Files.createFile():建立文件

Files.createDirectory(): 建立文件夹

Files.createDirectories(): 建立文件夹

public class FilesDemo2 {

public static void main(String[] args) throws IOException {
    Path path= Paths.get("demo7.txt");
    if(!Files.exists(path)){
        Files.createFile(path);
    }

    Path path2=Paths.get("demo4");
    if(!Files.exists(path2)){
        Files.createDirectory(path2);
    }

    Path path3=Paths.get("demo5\\test");
    if(!Files.exists(path3)){
        Files.createDirectories(path3);
    }
}

}
删除文件或目录
Files.delete():删除一个文件或目录

public class FilesDemo3 {

public static void main(String[] args) throws IOException {
    Path path= Paths.get("demo7.txt");
    Files.delete(path);
}

}
把一个文件从一个地址复制到另外一个位置
Files.copy():把一个文件从一个地址复制到另外一个位置

public class FilesDemo4 {

public static void main(String[] args) throws IOException {
    Path srcPath= Paths.get("demo6.txt");
    Path destPath=Paths.get("demo7.txt");

    //Files.copy(srcPath,destPath);

    //强制覆盖已经存在的目标文件
    Files.copy(srcPath,destPath, StandardCopyOption.REPLACE_EXISTING);
}

}
获取文件属性
public class FilesDemo5 {

public static void main(String[] args) throws IOException {
    Path path= Paths.get("demo7.txt");

    System.out.println(Files.getLastModifiedTime(path));
    System.out.println(Files.size(path));
    System.out.println(Files.isSymbolicLink(path));
    System.out.println(Files.isDirectory(path));
    System.out.println(Files.readAttributes(path,"*"));
}

}
遍历一个文件夹
public class FilesDemo6 {

public static void main(String[] args) throws IOException {
    Path path= Paths.get("demo3\\demo2");

    DirectoryStream<Path> paths=Files.newDirectoryStream(path);
    for(Path p:paths){
        System.out.println(p.getFileName());
    }
}

}
遍历整个文件目录
FileVisitor须要调用方自行实现,而后做为参数传入walkFileTree(); FileVisitor的每一个方法会在遍历过程当中被调用屡次。

public class FilesDemo7 {

public static void main(String[] args) throws IOException {
    Path path= Paths.get("demo3\\demo2");

    List<Path> paths=new ArrayList<>();
    Files.walkFileTree(path,new FileVisitor(paths));
    System.out.println("paths:"+paths);
}

private static class FileVisitor extends SimpleFileVisitor<Path> {
    private List<Path> paths;

    public FileVisitor(List<Path> paths){
        this.paths=paths;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        if(file.toString().endsWith(".txt")){
            paths.add(file.getFileName());
        }
        return super.visitFile(file, attrs);
    }
}

}
输出结果:

paths:[a.txt, test2.txt, test.txt, test3.txt]
免费Java高级资料须要本身领取,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo高并发分布式等教程,一共30G。
传送门:https://mp.weixin.qq.com/s/Jz...

相关文章
相关标签/搜索