(JavaSE 学习记录) IO之文件字节流 FileInputStream、FileOutputStream,利用文件字节流拷贝文件

文件字节流

FileInputStream:字节方式读取文件(适合读取所有类型文件
FileOutputStream:字节方式输出到文件中

IO基本步骤:
1、创建源
2、选择流
3、操作
4、关闭流

FileInputStream
        //创建源
        File src = new File("Garden.txt");
        //选择流
        InputStream is = null;
        //操作
        try {
            is = new FileInputStream(src);
            byte[] flush = new byte[1024];
            int len;	//实际长度
            //read(byte[]);返回读到的数据的长度
            //read();返回char,一个一个读
            //此处为纯文本,将其打印出来
            while((len = is.read(flush))!=-1){
                String temp = new String(flush,0,len);
                System.out.println(temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭流
            //null的判断不要忘
            try {
                if (null!=is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
FileOutputStream
        File dest = new File("dest02.txt");
        OutputStream os = null;
        try {
            os = new FileOutputStream(dest,true);
			//操作的是字节,需要将内容转化为字节数组
            String str = "Upsy Daisy";
            byte[] flush = str.getBytes();
            os.write(flush,0,str.length());
            os.flush();	//记得flush
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (null!=os) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
利用文件字节流拷贝文件

注意先开的流后关闭,且要分别关闭。

        File src = new File("Garden.txt");
        File dest = new File("GardenCopy.txt");

        InputStream is = null;
        OutputStream os = null;

        try {
            is = new FileInputStream(src);
            os = new FileOutputStream(dest);

            byte[] flush = new byte[1024];
            int len;
            //读、写
            while ((len = is.read(flush))!=-1){
                    os.write(flush,0,len);
                    os.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (null!=os) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (null!=is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值