一 , FileChanle

package nio;

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

/**
 * @author DELL
 */
public class TestChannel {
    public static void main(String[] args) throws IOException {
//        read();
//        write();
//        transferFrom();
        transferTo();
    }

    /**
     * 读取
     * @throws IOException
     */
    public static void read() throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\a.txt", "rw");
        FileChannel channel = randomAccessFile.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(2);
        int read = channel.read(byteBuffer);
        while (read!=-1){
            byteBuffer.flip();
            while (byteBuffer.hasRemaining()){
                System.out.print((char)byteBuffer.get());
            }
            byteBuffer.clear();
            read = channel.read(byteBuffer);
        }

        System.out.println();
        channel.close();
        randomAccessFile.close();
    }

    /**
     * 写入
     * @throws IOException
     */
    public static void write() throws IOException{

        RandomAccessFile readFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\素书.txt", "rw");
        RandomAccessFile writeFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\b.txt", "rw");

        FileChannel readFileChannel = readFile.getChannel();
        FileChannel writeFileChannel = writeFile.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1);

        int read = readFileChannel.read(buffer);

        while (read!=-1){
            buffer.flip();
            while (buffer.hasRemaining()){
                writeFileChannel.write(buffer);
            }
            buffer.clear();
            read = readFileChannel.read(buffer);
        }

        readFileChannel.close();
        writeFileChannel.close();

    }

    /**
     * 通道之间传输数据
     */
    public static void transferTo () throws IOException{
        RandomAccessFile readFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\素书.txt", "rw");
        RandomAccessFile writeFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\b.txt", "rw");

        FileChannel readFileChannel = readFile.getChannel();
        FileChannel writeFileChannel = writeFile.getChannel();

        readFileChannel.transferTo(0,readFileChannel.size(),writeFileChannel);

        readFileChannel.close();
        writeFileChannel.close();
    }

    /**
     * 通道之间传输数据
     */
    public static void transferFrom () throws IOException{
        RandomAccessFile readFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\素书.txt", "rw");
        RandomAccessFile writeFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\b.txt", "rw");

        FileChannel readFileChannel = readFile.getChannel();
        FileChannel writeFileChannel = writeFile.getChannel();

        writeFileChannel.transferFrom(readFileChannel,0,readFileChannel.size());

        readFileChannel.close();
        writeFileChannel.close();
    }

}

 

posted @ 2022-08-16 19:47  武卡卡  阅读(19)  评论(0编辑  收藏  举报