IO流--

该博客展示了如何使用Java NIO(New IO)的FileChannel和ByteBuffer实现文件内容的高效复制。通过创建两个线程,一个读取源文件,另一个写入目标文件,共享同一缓冲区实现数据传输。示例代码详细地展示了读写过程,并用锁和条件变量确保线程同步。
摘要由CSDN通过智能技术生成

java 中IO流是基础,包括文件IO,网络IO,控制台,数据库连接等。
从流的格式分类上有:
字节流
过滤的字节流:可以进行压缩等改变字节的操作
字符流(Reader,Writer)

类图:
在这里插入图片描述由类图结构可以看到,SockertInputStream 继承了FileInputStream ,也属于一切皆文件的描述,可以使用fd创建流。

FileChannel (通道)从流获取,Channel,Buffer 是NIO(new ) api

来个代码例子,使用FileChannel ,公用同一个buffer,将一个文件中的内容不断向另一个文件拷贝。

package test.java.io;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class StreamTest {
    public static void main(String[] args) throws IOException, InterruptedException {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byte[] bytes = new byte[1028];
        String l = "l";
        Lock lock = new ReentrantLock();
        Condition pCondition = lock.newCondition();
        Condition cCondition = lock.newCondition();

        Thread t1 = new Thread(() -> {
            FileInputStream in = null;
            try {
                in = new FileInputStream("/Users/a.text");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            FileChannel fileChannel = in.getChannel();
            while (true) {
                try {
                    lock.lock();
                    byteBuffer.clear();
                    int i = fileChannel.read(byteBuffer);
                    byteBuffer.flip();
                    Thread.sleep(100);
                    System.out.println("in--->" + i + "   " + new String(byteBuffer.array()));
                    cCondition.signal();
                    pCondition.await();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }

            }
        }, "in");


        Thread oT = new Thread(() -> {
            FileOutputStream out = null;
            try {
                out = new FileOutputStream("/Users/a_copy.text");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            FileChannel outChannel = out.getChannel();
            while (true) {
                try {
                    lock.lock();
                    Thread.sleep(100);
                    System.out.println("out--->" + byteBuffer.arrayOffset() + new String(byteBuffer.array()));
                    outChannel.write(byteBuffer);
                    byteBuffer.clear();
                    pCondition.signal();
                    cCondition.await();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }

        }, "out");
        t1.start();
        oT.start();

        Thread.sleep(30000l);


    }

    private static void sysIn() throws IOException {
        InputStream in = System.in;
        byte[] bytes = new byte[1024];
        int i = 0;

        FileOutputStream outputStream = new FileOutputStream("/Users/a.text");
        while ((i = in.read()) != -1) {
            char a = (char) i;
            System.out.print(a);
            outputStream.write(a);
        }

//        while ((i = in.read(bytes)) != 0) {
            System.out.println(new String(bytes));
//            byte[] temp1 = new byte[i];
//            System.arraycopy(bytes, 0, temp1, 0, i);
//            System.out.println(new String(temp1));
//        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值