字节流复制文件

文件字节流复制文件

  • 因为引用字节流是直接使用二进制,所以文件字节流非常通用,可以用来操作字符的文档 ,还可以操作任何的其他类型文件,例如:图片、压缩包等等。
  • 编写一个程序,吧一个文件复制到指定文件夹下
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class CopyDemo {

    public static void copyTest(String inPath,String outPath) throws Exception {

        FileInputStream in = new FileInputStream(inPath);
        FileOutputStream out = new FileOutputStream(outPath);

        byte[] b = new byte[100];
        int len = 0;

        while ((len = in.read(b)) != -1) {
            //第一个参数为缓冲数组
            //第二个参数为从数组的哪里开始
            //第三个参数是获取的数组总长度
            out.write(b,0,len);//把数据写到内存中
        }
        out.flush();//把内存中的数据写到硬盘中
        //关闭流
        out.close();
        in.close();
    }

    public static void main(String[] args) throws Exception {
        CopyDemo.copyTest("D:\\JAVA\\Java 基础入门\\IO 流\\test\\t1.txt",//源文件路径
                "D:\\JAVA\\Java 基础入门\\IO 流\\test\\demo\\t1.txt");//复制到的路径
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值