日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

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

作者:求根知底 更新時(shí)間: 2022-07-16 編程語(yǔ)言

官方文檔如下表示
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.
創(chuàng)建文件輸出流以寫(xiě)入具有指定名稱(chēng)的文件。如果第二個(gè)參數(shù)為true,則字節(jié)將寫(xiě)入文件的末尾而不是開(kāi)頭。
自己的表述
true表示,每次new對(duì)象時(shí)會(huì)繼承上次new完之后的數(shù)據(jù),在上次寫(xiě)完的數(shù)據(jù)追加繼續(xù)寫(xiě)。如果false,每次寫(xiě)數(shù)據(jù)都會(huì)清除上次new完對(duì)象運(yùn)行的數(shù)據(jù),再接著寫(xiě)入。
代碼展示
參數(shù)為true(new兩次對(duì)象的話(huà),也就是運(yùn)行兩次)

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

結(jié)果展示
在這里插入圖片描述

測(cè)試之前,我先把test2.txt清空,方便結(jié)果展示
參數(shù)為false(運(yùn)行兩次)

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

結(jié)果展示
在這里插入圖片描述
這是我對(duì)這個(gè)參數(shù)的理解,大家遇到問(wèn)題多看官方文檔,但官方文檔畢竟是英文的,有時(shí)候翻譯成中文,有點(diǎn)迷糊。這時(shí)候大家應(yīng)該集思廣益,多去查資料,多實(shí)踐,實(shí)踐出新知!加油

原文鏈接:https://blog.csdn.net/weixin_47241585/article/details/125810431

欄目分類(lèi)
最近更新