0
点赞
收藏
分享

微信扫一扫

文件字节IO流案例--文件拷贝

水墨_青花 2022-02-06 阅读 93
package io;

import java.io.*;

/**
 * 学会使用字节流完成文件的复制(支持一切文件的复制)
 */

public class Demo01 {

    public static void main(String[] args){

        //创建字节流入管道接通
        try {
            InputStream is = new FileInputStream("D:\\myCode\\Data01.txt");   //文件路径

            //创建字节流出管道接通
            OutputStream os = new FileOutputStream("D:\\myCode\\Data02.txt");

            //定义一个字节数组转移数据
            byte[] buffer = new byte[1024];
            int len;
            while((len=is.read(buffer)) != -1){
                os.write(buffer,0,len);
            }
            System.out.println("复制完成");

            //关闭流
            os.close();
            is.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
举报

相关推荐

0 条评论