Netty学习01-通道和缓冲区的使用

使用通道和缓存区将数据写入文件中

package com.atguigu;

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

public class NIOFileChannel01
{
    public static void main(String[] args) throws IOException
    {
        String str = "hello 马钊!";
//        创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream("F:\\file02.txt");
//        根据输出流获取通道
        FileChannel fileChannel = fileOutputStream.getChannel();
//        创建一个缓冲区bytebuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//        将str放入到buffer
        byteBuffer.put(str.getBytes());
//        要对通道进行一个flip,进行读写转换
        byteBuffer.flip();

//        将ByteBuffer中的数据写入到fileChannel中
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
    }
}

将目标文件写入通道和缓存区

package com.atguigu;

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

public class NIOFileChannel02
{
    public static void main(String[] args) throws IOException
    {
//        创建文件的输入流
        File file = new File("f://file02.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

//        通过输入流FileInputStream获取对应的FIleChannel -> 实际类型 fileChannel
        FileChannel channel = fileInputStream.getChannel();
//        创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());

//        将通道的数据读入到buffer
        channel.read(byteBuffer);

//        将ByteBuffer的字节数据转成String
        System.out.println(new String(byteBuffer.array()));

//        关闭输入流
        fileInputStream.close();

    }
}

使用通道和缓存区对文件进行拷贝

package com.atguigu;

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

public class NIOFileChannel03
{
    public static void main(String[] args) throws IOException
    {
//        创建文件的输入流并且获得通道
        FileInputStream fileInputStream = new FileInputStream("1.txt");
        FileChannel fileChannel01 = fileInputStream.getChannel();

//        创建文件输出流并且获得通道
        FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
        FileChannel fileChannel02 = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);

        while (true)
        {
//            这里有个很重要的操作,就是要清空缓冲区!
            byteBuffer.clear(); //重置标志位
        /*
            public final Buffer clear() {
                position = 0;
                limit = capacity;
                mark = -1;
                return this;
            }
         */
            int read = fileChannel01.read(byteBuffer);
            if (read == -1)
            {
                break;
            }
//            将buffer中的数据写入到fileChannel02 -- file04.txt
//            要将缓冲区进行一个反转
            byteBuffer.flip();
            fileChannel02.write(byteBuffer);
        }
//        关闭输入流
        fileInputStream.close();
        fileOutputStream.close();

    }
}

使用transferFrom实现通道和通道之间的拷贝

package com.atguigu.nio;

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

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

        //创建相关流
        FileInputStream fileInputStream = new FileInputStream("f:\\file04.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("f:\\file05.txt");

        //获取各个流对应的filechannel
        FileChannel sourceCh = fileInputStream.getChannel();
        FileChannel destCh = fileOutputStream.getChannel();

        //使用transferForm完成拷贝
        destCh.transferFrom(sourceCh,0,sourceCh.size());
        //关闭相关通道和流
        sourceCh.close();
        destCh.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}

你可能感兴趣的:(Netty学习之路)