new FileOutputStream(“data\\test2.txt“,true),true是什么意思

官方文档如下表示
Creates a file output stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
创建文件输出流以写入具有指定名称的文件。如果第二个参数为true,则字节将写入文件的末尾而不是开头。
自己的表述
true表示,每次new对象时会继承上次new完之后的数据,在上次写完的数据追加继续写。如果false,每次写数据都会清除上次new完对象运行的数据,再接着写入。
代码展示
参数为true(new两次对象的话,也就是运行两次)

static void test1() {
        try (var fos = new FileOutputStream("data\\test2.txt",true)) {
            fos.write('D');
            fos.write('b');
            System.out.println("写入成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            //close会用
            e.printStackTrace();
        }
    }

结果展示
在这里插入图片描述

测试之前,我先把test2.txt清空,方便结果展示
参数为false(运行两次)

static void test1() {
        try (var fos = new FileOutputStream("data\\test2.txt",false)) {
            fos.write('D');
            fos.write('b');
            System.out.println("写入成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            //close会用
            e.printStackTrace();
        }
    }

结果展示
在这里插入图片描述
这是我对这个参数的理解,大家遇到问题多看官方文档,但官方文档毕竟是英文的,有时候翻译成中文,有点迷糊。这时候大家应该集思广益,多去查资料,多实践,实践出新知!加油

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值