java NIO之Pipe

1、简介

管道是 2 个线程之间的单向数据连接
一个 source 通道(读取)和一个sink 通道(写入)

1.1、常用方法

打开管道

Pipe pipe = Pipe.open();

 写入管道
需要访问 sink 通道

Pipe.SinkChannel sinkChannel = pipe.sink();

读取数据
需要访问 source 通道

Pipe.SourceChannel sourceChannel = pipe.source();

实战代码

完整代码如下:

  • 创建管道之后在创建sink管道
  • 并且设置缓冲区将其写入
  • 获取source管道并且读取数据
public class PipeDemo {

    public static void main(String[] args) throws IOException {
        //1 获取管道
        Pipe pipe = Pipe.open();

        //2 获取sink通道
        Pipe.SinkChannel sinkChannel = pipe.sink();

        //3 创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put("码农研究僧".getBytes());
        byteBuffer.flip();

        //4 写入数据
        sinkChannel.write(byteBuffer);

        //5 获取source通道
        Pipe.SourceChannel sourceChannel = pipe.source();

        //6 读取数据
        ByteBuffer byteBuffer2 = ByteBuffer.allocate(1024);

        //byteBuffer.flip();

        int length = sourceChannel.read(byteBuffer2);

        System.out.println(new String(byteBuffer2.array(),0,length));

        //7 关闭通道
        sourceChannel.close();
        sinkChannel.close();
    }

}


2、FileLock

文件锁是进程级别的,不是线程级别的

具体分为两大类:

**排它锁:**又叫独占锁。对文件加排它锁后,该进程可以对此文件进行读写,该进程独占此文件,其他进程不能读写此文件,直到该进程释放文件锁。

**共享锁:**某个进程对文件加共享锁,其他进程也可以访问此文件,但这些进程都只能读此文件,不能写。线程是安全的。只要还有一个进程持有共享锁,此文件就只能读,不能写

//创建 FileChannel 对象,文件锁只能通过 FileChannel 对象来使用
FileChannel fileChannel=new FileOutputStream("./1.txt").getChannel();
//对文件加锁
FileLock lock=fileChannel.lock();
//对此文件进行一些读写操作。
//.......
//释放锁
lock.release();

2.1、常用方法

lock是阻塞,trylock是非阻塞

获取文件锁的方法有如下:

  • lock() //对整个文件加锁,默认为排它锁。
  • lock(long position, long size, booean shared) //自定义加锁方式。
    前 2 个参数指定要加锁的部分(可以只对此文件的部分内容加锁),第三个参数值指定是否是共享锁
  • tryLock() //对整个文件加锁,默认为排它锁。
  • tryLock(long position, long size, booean shared) //自定义加锁方式。
    如果指定为共享锁,则其它进程可读此文件,所有进程均不能写此文件,如果某进程试图对此文件进行写操作,会抛出异常

FileLock另外两个方法:

  • boolean isShared() //此文件锁是否是共享锁
  • boolean isValid() //此文件锁是否还有效


2.2、代码实战

path,StandardOpenOption.WRITE,StandardOpenOption.APPEND分别是路径,读写数据,追加数据

public class FileLockDemo1 {

    public static void main(String[] args) throws Exception {
        String input = "manongyanjiuseng";
        System.out.println("input:"+input);

        ByteBuffer buffer = ByteBuffer.wrap(input.getBytes());

        String filePath = "b//1.txt";
        Path path = Paths.get(filePath);

        FileChannel channel =
                FileChannel.open(path,
                        StandardOpenOption.WRITE,StandardOpenOption.APPEND);
        channel.position(channel.size()-1);

        //加锁
        FileLock lock = channel.lock(0L,Long.MAX_VALUE,true);
        System.out.println("是否共享锁:"+lock.isShared());

        channel.write(buffer);
        channel.close();

        //读文件
        readFile(filePath);
    }

    private static void readFile(String filePath) throws Exception {
        FileReader fileReader = new FileReader(filePath);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String tr = bufferedReader.readLine();
        System.out.println("读取出内容:");
        while(tr != null) {
            System.out.println(" "+tr);
            tr = bufferedReader.readLine();
        }
        fileReader.close();
        bufferedReader.close();
    }
}


3、Java NIO(其他)

此处包括了path和files等


3.1、Path

  • 一个路径可以指向一个文件或一个目录。路径可以是绝对路径,也可以是相对路径
  • 使用 java.nio.file.Path 实例必须创建一个 Path 实例。可以使用 Paths 类(java.nio.file.Paths)中的静态方法 Paths.get()来创建路径实例

也是当前的绝对路径

Path path = Paths.get("b://1.txt");

也可以创建相对路径,使用 Paths.get(basePath, relativePath)方法创建一个相对路径

Path path = Paths.get("b://txt”,“1.txt");
  • 使路径标准化。标准化意味着它将移除所有在路径字符串的中间的.和…代码,并解析路径字符串所引用的路径Path.normalize()
String originalPath = "b://projects//..//yygh-project";

Path path1 = Paths.get(originalPath);
System.out.println("path1 = " + path1);

Path path2 = path1.normalize();
System.out.println("path2 = " + path2);

3.2、Files

Files.createDirectory()根据 Path 实例创建一个新目录

Path path = Paths.get("b://1");
try {
    Path directory = Files.createDirectory(path);
} catch (IOException e) {
    e.printStackTrace();
}
  • 复制,一个路径拷贝一个文件到另外一个目录
  • 第三个参数可加可不加,Files.copy()方法的第三个参数。如果目标文件已经存在,这个参数指示 copy()方法覆盖现有的文件
Path path1 = Paths.get("b://1.txt");
Path path2 = Paths.get("b://2.txt");
try {
    Path copy = Files.copy(path1, path2, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
    e.printStackTrace();
}
  • 移动文件既可以移动到不同的目录,也可以在相同的操作中更改它的名称
  • Files.move()的第三个参数。这个参数告诉 Files.move()方法来覆盖目标路径上的任何现有文件
Path sourcePath = Paths.get("b://1.txt");
Path destinationPath = Paths.get("b://2.txt");

try {
    Files.move(sourcePath, destinationPath,
            StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
    //移动文件失败
    e.printStackTrace();
}
  • 删除一个文件或者目录
Path path = Paths.get("b://1.txt");
try {
    Files.delete(path);
} catch (IOException e) {
    // 删除文件失败
    e.printStackTrace();
}


3.3、AsynchronousFileChannel

异步地将数据写入文件

  • 静态方法 open()创建
    第二个参数是一个或多个打开选项,它告诉 AsynchronousFileChannel 在文件上执行什么操作。在本例中,我们使用StandardOpenOption.READ 选项,表示该文件将被打开阅读
  • 读取数据
    第一种方式是调用返回Future 的 read()方法
    第二种方法是调用 read()方法,该方法将一个 CompletionHandler 作为参数

(1)创建了一个 AsynchronousFileChannel
(2)创建一个 ByteBuffer,它被传递给 read()方法作为参数,以及一个 0 的位置。
(3)在调用 read()之后,循环,直到返回的 isDone()方法返回 true。
(4)读取操作完成后,数据读取到 ByteBuffer 中,然后打印到 System.out 中

@Test
public void readAsyncFileChannelFuture() throws Exception {
    //1 创建AsynchronousFileChannel
    Path path = Paths.get("b://1.txt");
    AsynchronousFileChannel fileChannel =
            AsynchronousFileChannel.open(path, StandardOpenOption.READ);

    //2 创建Buffer
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    //3 调用channel的read方法得到Future
    Future<Integer> future = fileChannel.read(buffer, 0);

    //4 判断是否完成 isDone,返回true
    while(!future.isDone());

    //5 读取数据到buffer里面
    buffer.flip();
//        while(buffer.remaining()>0) {
//            System.out.println(buffer.get());
//        }
    byte[] data = new byte[buffer.limit()];
    buffer.get(data);
    System.out.println(new String(data));
    buffer.clear();

}

(1)读取操作完成,将调用 CompletionHandler 的 completed()方法。
(2)对于 completed()方法的参数传递一个整数,它告诉我们读取了多少字节,以及传递给 read()方法的“附件”。“附件”是 read()方法的第三个参数。在本代码中,它是 ByteBuffer,数据也被读取。
(3)如果读取操作失败,则将调用 CompletionHandler 的 failed()方法

@Test
public void readAsyncFileChannelComplate() throws Exception {
    //1 创建AsynchronousFileChannel
    Path path = Paths.get("b://1.txt");
    AsynchronousFileChannel fileChannel =
            AsynchronousFileChannel.open(path, StandardOpenOption.READ);

    //2 创建Buffer
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    //3 调用channel的read方法得到Future
    fileChannel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
        @Override
        public void completed(Integer result, ByteBuffer attachment) {
            System.out.println("result: "+result);

            attachment.flip();
            byte[] data = new byte[attachment.limit()];
            attachment.get(data);
            System.out.println(new String(data));
            attachment.clear();
        }

        @Override
        public void failed(Throwable exc, ByteBuffer attachment) {

        }
    });
}

通过 Future 写数据

@Test
public void writeAsyncFileFuture() throws IOException {
    //1 创建AsynchronousFileChannel
    Path path = Paths.get("b://1.txt");
    AsynchronousFileChannel fileChannel =
            AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);

    //2 创建Buffer
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    //3 write方法
    buffer.put("atguigu ".getBytes());
    buffer.flip();
    Future<Integer> future = fileChannel.write(buffer, 0);

    while(!future.isDone());

    buffer.clear();
    System.out.println("write over");
}

通过 CompletionHandler 写数据

@Test
public void writeAsyncFileComplate() throws IOException {
    //1 创建AsynchronousFileChannel
    Path path = Paths.get("b://1.txt");
    AsynchronousFileChannel fileChannel =
            AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);

    //2 创建Buffer
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    //3 write方法
    buffer.put("atguigujavajava".getBytes());
    buffer.flip();

    fileChannel.write(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
        @Override
        public void completed(Integer result, ByteBuffer attachment) {
            System.out.println("bytes written: " + result);
        }

        @Override
        public void failed(Throwable exc, ByteBuffer attachment) {

        }
    });

    System.out.println("write over");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值