JAVA中的FileOutputStream和文件复制

JAVA中的FileOutputStream

  • FileOutputStream使用重点:
    1、使用方法
    2、FileOutputStream构造方法中的append参数
    3、写入String字符串的方法
    4、强制输出缓存区内剩余数据,清空缓存区
public class FileOutputStreamTest01 {
    public static void main(String[] args){
        FileOutputStream fos = null;
        try {
            //第二个参数:append,为true时将以追加的方式在文件末尾写入
            fos = new FileOutputStream("src/io/tempOutput.txt",true);
            
            //写入数组的byte是ASCII码,会自动转为对应的字节
            byte[] bytes = {100,101,102};
            
            //使用write方法要谨慎,直接write()会将已有的文件清空,再写入数据
            //如果没有目标文件,则会新建文件
            fos.write(bytes);
            //将byte数组一部分写出
            fos.write(bytes,0,2);

            //写入String字符串
            String strings = "我有一只小乌龟!";
            //string数据类型转为byte[]数组
            byte[] bytes1 = strings.getBytes();
            fos.write(bytes1);

            //写完之后一定要刷新
            fos.flush();



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos != null) {
                try {
                    fos.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

文件复制

  • 使用FileOutputStream和FileInputStream完成文件的复制:
    使用该方法拷贝文件时,文件类型随意,所有文件都可拷贝

  • finally处两个close方法要分开写,一起try的话其中一个出现异常,可能会影响到另一个的关闭

public class FileOutputStreamTest02 {
    public static void main(String[] args) {
        FileInputStream fls = null;
        FileOutputStream fos = null;
        try {
            //指定输入输出的文件
            fls = new FileInputStream("src\\readme");
            fos = new FileOutputStream("src\\io\\copiedfile.txt");
//            //一次复制1KB
           byte[] bytes = new byte[1024];
            //一次读取的1024个字节转为String时可能会发生拼接错误
//            byte[] bytes = new byte[fls.available()];
            int readCount;
            //边读边写
            while ((readCount = fls.read(bytes)) != -1){
                fos.write(bytes,0,readCount);
            }
            //flush
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //分开try,一起try的话其中一个出现异常,可能会影响到另一个的关闭
            if (fls != null){
                try {
                    fls.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值