網(wǎng)站首頁(yè) 編程語(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)建文件輸出流以寫入具有指定名稱的文件。如果第二個(gè)參數(shù)為true,則字節(jié)將寫入文件的末尾而不是開頭。
自己的表述
true表示,每次new對(duì)象時(shí)會(huì)繼承上次new完之后的數(shù)據(jù),在上次寫完的數(shù)據(jù)追加繼續(xù)寫。如果false,每次寫數(shù)據(jù)都會(huì)清除上次new完對(duì)象運(yùn)行的數(shù)據(jù),再接著寫入。
代碼展示
參數(shù)為true(new兩次對(duì)象的話,也就是運(yùn)行兩次)
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會(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("寫入成功");
} 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
相關(guān)推薦
- 2022-03-06 Linux之Centos8創(chuàng)建CA證書教程_Linux
- 2022-07-23 Python代碼實(shí)現(xiàn)雙鏈表_python
- 2022-07-08 python中的type,元類,類,對(duì)象用法_python
- 2022-10-18 C++數(shù)據(jù)結(jié)構(gòu)之二叉搜索樹的實(shí)現(xiàn)詳解_C 語(yǔ)言
- 2023-02-10 一文教會(huì)你用Python實(shí)現(xiàn)pdf轉(zhuǎn)word_python
- 2022-06-01 python中使用正則表達(dá)式的方法詳解_python
- 2022-04-04 react報(bào)錯(cuò)export ‘Switch‘ (imported as ‘Switch‘) was
- 2022-02-01 在linux環(huán)境下com.aspose.words將word文件轉(zhuǎn)為pdf后亂碼,window環(huán)境下
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支