JAVA NIO 操作文件

本文介绍使用Java NIO进行文件复制的不同方法,包括非直接缓冲区复制、内存映射文件复制及直接通过通道传输数据的方式。这些方法提高了文件复制的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import org.junit.Test;

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

import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.channels.FileChannel.MapMode;

public class Demo {


    @Test
    public void test(){

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        byteBuffer.put("abcde".getBytes());

        System.out.println(byteBuffer.position());

        System.out.println(new String(byteBuffer.array(),0,byteBuffer.position()));


    }

    /**
     *
     * 利用通道文件复制
     * 非直接缓冲区
     */
    @Test
    public void test1() {

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
                fileInputStream = new FileInputStream("1.txt");
                fileOutputStream = new FileOutputStream("2.txt");

                inputChannel = fileInputStream.getChannel();
                outputChannel = fileOutputStream.getChannel();

                ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

                while (inputChannel.read(byteBuffer) != -1) {
                    byteBuffer.flip();
                    System.out.println(new String(byteBuffer.array(), 0, byteBuffer.limit()));
                    outputChannel.write(byteBuffer);
                    byteBuffer.clear();
                }

        }catch (Exception e){

        }
        finally {
            try{
                if (outputChannel != null){
                    outputChannel.close();
                }
            }
            catch (Exception e){}
            try{
                if (inputChannel != null){
                    inputChannel.close();
                }
            }
            catch (Exception e){}try{
                if (fileOutputStream != null){
                    fileOutputStream.close();
                }
            }
            catch (Exception e){}try{
                if (fileInputStream != null){
                    fileInputStream.close();
                }
            }
            catch (Exception e){}


        }
    }

    /**
     *
     *利用直接缓冲区完成文件的复制(内存映射文件)
     */
    @Test

    public void test2() throws IOException {
        FileChannel inputChannel = FileChannel.open(Paths.get("1.txt"), StandardOpenOption.READ);
        FileChannel outputChannel = FileChannel.open(Paths.get("2.txt"), StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);


        MappedByteBuffer inputMapperByteBuffer = inputChannel.map(MapMode.READ_ONLY,0,inputChannel.size());
        MappedByteBuffer outputMapperByteBuffer = outputChannel.map(MapMode.READ_WRITE,0,inputChannel.size());

        byte[] bytes = new byte[inputMapperByteBuffer.limit()];

        inputMapperByteBuffer.get(bytes);

        outputMapperByteBuffer.put(bytes);

        outputChannel.close();
        inputChannel.close();
        outputChannel.close();
        inputChannel.close();
    }


    /**
     *
     * 直接通过通道传输数据,也为直接缓冲区传输
     * @throws IOException
     */
    @Test
    public void test3() throws IOException {
        FileChannel inputChannel = FileChannel.open(Paths.get("1.txt"), StandardOpenOption.READ);
        FileChannel outputChannel = FileChannel.open(Paths.get("2.txt"), StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);

        inputChannel.transferTo(0,inputChannel.size(),outputChannel);

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值