1-NIO使用

1.FileChannel 和 Buffer

package nio._Buffer;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Demo {
    public static void main(String[] args) throws IOException {
        RandomAccessFile aFile = new RandomAccessFile("f:/zhuopeng/nioFile.txt","rw");
        FileChannel fileChannel = aFile.getChannel();


        RandomAccessFile aFile2 = new RandomAccessFile("f:/zhuopeng/nioFile2.txt","rw");
        FileChannel fileChannel2 = aFile2.getChannel();

        //分配内存,创建一个buffer,capacity为48
        ByteBuffer buf = ByteBuffer.allocate(48);

        //1.放入缓存的方法,从channel读取
        int read = fileChannel.read(buf);

        //2.放入缓存的方法,自己手动放进去
        String str = "zhuopeng";
        byte[] b = str.getBytes();
        buf.put(b);

        while (read != -1) {
            //切换为读模式
            buf.flip();
            //标记当前的position
            buf.mark();
            while (buf.hasRemaining()){
                System.out.print((char)buf.get());//一次读取一个byte
            }
            //重新回到当前的position
            buf.reset();
            //将buf中的内容写到另一个channel中
            int write = fileChannel2.write(buf);

            buf.clear();
            read = fileChannel.read(buf);
        }
        aFile.close();

    }
}

 

 2. Scattering 和 Gathering,以下是Scatter,Gather和Scatter差不多

package nio._Buffer;

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

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

        
        RandomAccessFile file1 = new RandomAccessFile("f:/zhuopeng/nioFile.txt","rw");
        
        FileChannel fileChannel = file1.getChannel();

        //构造两个buffer,并且加入到一个数组中
        ByteBuffer buf1 = ByteBuffer.allocate(5);
        ByteBuffer buf2 = ByteBuffer.allocate(20);
        ByteBuffer[] bufArr = {buf1,buf2};

        long read = fileChannel.read(bufArr);

        while(read != -1){
            buf1.flip();
            buf2.flip();
            //读取第一个buf
            System.out.println("第一个buf结果:");
            while (buf1.hasRemaining()){
                System.out.print((char)buf1.get());//一次读取一个byte
            }
            System.out.println("");
            System.out.println("第二个buf结果显示:");
            while (buf2.hasRemaining()){
                System.out.print((char)buf2.get());
            }

            
            read = -1;
        }




    }
}

 

2.Channel Transfer

public class _ChannelTransfer {
    public static void main(String[] args) throws IOException {
        RandomAccessFile file1 = new RandomAccessFile("nioFile.txt", "rw");
        RandomAccessFile file2 = new RandomAccessFile("nioFile3.txt", "rw");

        FileChannel fileChannel1 = file1.getChannel();
        FileChannel fileChannel2 = file2.getChannel();
        long size = fileChannel1.size();

        fileChannel2.transferFrom(fileChannel1,0,size);
        fileChannel1.close();
        fileChannel2.close();
        file1.close();
        file2.close();
    }
}

 

4.Selector

posted @ 2018-12-09 18:31  式微胡不归  阅读(189)  评论(0编辑  收藏  举报