Java文件拷贝

Java文件拷贝

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class CopyTest {
    public static void main(String[] args) throws IOException {
        String src = "E:\\1.txt";
        String target = "E:\\2.txt";
        //copyByBIO(src,target);
        //copyByNIO(src,target);
        copyByNIO2(src,target);
    }

    /**
     * @param src   要拷贝内容的源文件路径
     * @param target    要拷贝文件到达的目标路径
     * @throws IOException
     */
    private static void copyByBIO(String src,String target) throws IOException {
        //指定读取的文件位置
        File file = new File(src);
        //创建输入流
        FileInputStream fileInputStream = new FileInputStream(file);

        //指定输出的文件位置
        File file1 = new File(target);
        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream(file1);

        //拷贝,一边读一边写。int实际上是ASCII码
        int b =0;
        while((b=fileInputStream.read())!=-1){
            fileOutputStream.write(b);
            System.out.println(b);
        }

        //关流
        fileInputStream.close();
        fileOutputStream.close();
    }


    /**
     * @param src   要拷贝内容的源文件路径
     * @param target    要拷贝文件到达的目标路径
     * @throws IOException
     */
    private static void copyByNIO(String src,String target)throws IOException{
        //指定读取文件位置
        File file = new File(src);
        //创建文件输入流
        FileInputStream fileInputStream = new FileInputStream(file);
        //获得信道channel
        FileChannel channel = fileInputStream.getChannel();

        //指定输出的文件位置
        File file1 = new File(target);
        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream(file1);
        //获得信道channel
        FileChannel channel2 = fileOutputStream.getChannel();

        //缓冲区,指定容量为文件file的长度
        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());

        //拷贝
        //通过输入流读取数据到缓冲区
        channel.read(byteBuffer);

        //读写交换
        byteBuffer.flip();

        //通过输出流把缓冲区的数据写入指定位置
        channel2.write(byteBuffer);

        //关流
        channel2.close();
        fileOutputStream.close();
        channel.close();
        fileInputStream.close();
    }

    /**
     * @param src   要拷贝内容的源文件路径
     * @param target    要拷贝文件到达的目标路径
     * @throws IOException
     */
    private static void copyByNIO2(String src,String target)throws IOException{
        //创建文件输入流
        FileInputStream fileInputStream = new FileInputStream(new File(src));
        //获得信道channel
        FileChannel channel = fileInputStream.getChannel();

        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream(new File(target));
        //获得信道channel
        FileChannel channel2 = fileOutputStream.getChannel();

        //将channel中的数据传输到channel2
        channel2.transferFrom(channel,0,channel.size());

        //关流
        channel2.close();
        fileOutputStream.close();
        channel.close();
        fileInputStream.close();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值