netty 11-20 利用Buffer 和Channel 和ServerSocketChannel进行文件的复制,也就是读写,以及网络传输

用FileChanel 将字符串写入本地磁盘:

package nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class File01 {
    public static void main(String[] args) throws Exception {


        String str = "hello FileChannel";
        FileOutputStream fop = new FileOutputStream("d:\\file01.txt");
        FileChannel fileChannel = fop.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(str.getBytes());
        byteBuffer.flip();
        fileChannel.write(byteBuffer);
        fop.close();


    }
}

整体流程:
1,用到FileOutPutStream
2,getChannel
3,ByteBuffer 这个就是一个数据差不多,就是多了下标和一些方便于用的方法,容器。
4,数据是利用通道的方法,write写进流或者说是

为什么叫做输入流,其实就是将数据读取到流里面,输出流就是相反。

读取

package nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class File02 {
    public static void main(String[] args) throws Exception {

        File file = new File("d:\\file01.txt");
        FileInputStream ip = new FileInputStream(file);
        ByteBuffer bytebu = ByteBuffer.allocate((int) file.length());
        FileChannel channel = ip.getChannel();
        //将文件中的数据督导内存中。
        int count = channel.read(bytebu);
        System.out.println("new String(bytebu.array()) = " + new String(bytebu.array()));//.array()方法,就是拿到那个array保存数组
        ip.close();

    }
}

用一个buffer 来复制文件

package nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class File03 {
    public static void main(String[] args) throws Exception {

        FileInputStream ip = new FileInputStream("netty/1.txt");
        ByteBuffer bytebu = ByteBuffer.allocate(1234);
        FileChannel channel = ip.getChannel();
        FileOutputStream op = new FileOutputStream("netty/2.txt");
        FileChannel channel1 = op.getChannel();

        //将文件中的数据督导内存中。
        while (true) {
            bytebu.clear();//buffer的下标要从新清空,要不position = limit count=0 ,就无限循环了
            int count = channel.read(bytebu);
            if (count == -1) {
                break;
            }
            bytebu.flip();
            channel1.write(bytebu);
        }
        System.out.println("new String(bytebu.array()) = " + new String(bytebu.array()));//.array()方法,就是拿到那个array保存数组
        ip.close();
        op.close();

    }
}

直接通过channel来复制

package nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class File04 {
    public static void main(String[] args) throws Exception {

        FileInputStream ip = new FileInputStream("d:/1.jpg");
        FileChannel channel = ip.getChannel();
        FileOutputStream op = new FileOutputStream("d:/2.jpg");
        FileChannel channel1 = op.getChannel();
        channel1.transferFrom(channel, 0, channel.size());

        ip.close();
        op.close();

    }
}

ByteBuffer.putInt()就要
ByteBuffer.getInt()

byteBuffer.asReadOnlyBuffer() 就会变成一个只读的。

用计算机内存实现修改

package nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class File05 {
    public static void main(String[] args) throws Exception {

        RandomAccessFile ran = new RandomAccessFile("d:\\file01.txt","rw");

        FileChannel channel = ran.getChannel();
        //直接用计算机的内存,而不是jvm的内存来修改数据。
        MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
        map.put(0, (byte) 'X');//数字和字母对应的是一个字节
        map.put(4, (byte) 'Y');

        ran.close();
    }
}

我们用ServerSocketChannel 不需要流了,而且 用这个得到的socket 可读可写。因为socket是已经连接好的。自动接收返回的信息。

package nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Arrays;

public class File06 {
    public static void main(String[] args) throws IOException {

        //也就是不用流来操作了
        ServerSocketChannel open = ServerSocketChannel.open();
        InetSocketAddress inetSocketAddress = new InetSocketAddress(7890);
        //给Socket绑定端口号
        open.socket().bind(inetSocketAddress);

        ByteBuffer[] arr = new ByteBuffer[2];
        arr[0]=ByteBuffer.allocate(5);
        arr[1]=ByteBuffer.allocate(3);
        SocketChannel accept = open.accept();
            int fin = 8;
        while (true) {
            int count =0;
            while (count < fin) {
                long read = accept.read(arr);//返回的是读取的是写入的个数
                count+=read;
                System.out.println("count = " + count);
                Arrays.asList(arr).stream().map(e -> e.position()+":limit: "+e.limit()).forEach(System.out::println);
                Arrays.asList(arr).forEach(e -> e.flip());
            }
            
            
            int index = 0;
            while (index < fin) {
                long write = accept.write(arr);
                index+= write;
                Arrays.asList(arr).forEach(e ->e.clear());
                System.out.println("read = " + count +"index|"+index +" fin| "+fin);
            }


        }

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值