×

NIO Channel

30分钟学习NIO的Channel

我的笔记 我的笔记 发表于2020-05-31 21:42:48 浏览3389 评论7

7人参与发表评论

案例一:使用NIO的Channel输出文件

package com.fyd.nio;

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

/***
 * @Author付亚东
 * @Date 2020/5/31 10:24
 ****/
public class NIOFileChannel01 {
    public static void main(String[] args) throws Exception{
        String s = "hello word";
        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\test.txt");
        //通过filtoutputstream获取channel
        FileChannel fileChannel = fileOutputStream.getChannel();
        //创建一个缓冲器
        ByteBuffer  byteBuffer = ByteBuffer.allocate(1024);
        //把数据放入缓冲区
        byteBuffer.put(s.getBytes());
        //转换缓冲区状态
        byteBuffer.flip();
        //吧缓冲区数据写入channel
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
        fileChannel.close();
    }
}

案例二:使用NIO的channel读取文件

package com.fyd.nio;

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

/***
 * @Author付亚东
 * @Date 2020/5/31 10:24
 ****/
public class NIOFileChannel01 {
    public static void main(String[] args) throws Exception{
        String s = "hello word";
        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\test.txt");
        //通过filtoutputstream获取channel
        FileChannel fileChannel = fileOutputStream.getChannel();
        //创建一个缓冲器
        ByteBuffer  byteBuffer = ByteBuffer.allocate(1024);
        //把数据放入缓冲区
        byteBuffer.put(s.getBytes());
        //转换缓冲区状态
        byteBuffer.flip();
        //吧缓冲区数据写入channel
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
        fileChannel.close();
    }
}

案例三:使用NIO的channel并且使用同一个Buffer读取和写入文件

package com.fyd.nio;

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

/***
 * @Author付亚东
 * @Date 2020/5/31 10:42
 ****/
public class NIOFileChannel03 {
    public static void main(String[] args) throws  Exception{
        //首先读取一个文件
        File file = new File("d:\\test.txt");
        //获取输入流
        FileInputStream fileInputStream =new FileInputStream(file);
        //获取channel
        FileChannel fileChannel = fileInputStream.getChannel();
        //创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //获取一个新文件
        FileOutputStream fileOutputStream  =new FileOutputStream("d:\\test01.txt");
        FileChannel channel = fileOutputStream.getChannel();
        while (true){
            byteBuffer.clear();
            //读取
            int i =fileChannel.read(byteBuffer);
            if(i==-1){
                break;
            }
            byteBuffer.flip();
            channel.write(byteBuffer);
        }

        fileInputStream.close();
        fileOutputStream.close();
        fileChannel.close();
        channel.close();
    }
}

案例四:使用NIO的Channel复制内容(transferFrom)

package com.fyd.nio;

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

/***
 * @Author付亚东
 * @Date 2020/5/31 10:54
 ****/
public class NIOFIleChannel04 {
    public static void main(String[] args)throws Exception {
        File file = new File("d:\\test.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\test03.txt" );

        FileChannel channel = fileInputStream.getChannel();
        FileChannel channel1 = fileOutputStream.getChannel();
        channel1.transferFrom(channel,0,channel.size());
        fileInputStream.close();
        fileOutputStream.close();
        channel.close();
        channel1.close();
    }
}



我的笔记博客版权我的笔记博客版权