網(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)建文件輸出流以寫(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
相關(guān)推薦
- 2022-04-19 c#中token的使用方法實(shí)例_C#教程
- 2022-08-14 Oracle中dbms_output.put_line的用法實(shí)例_oracle
- 2022-02-20 Ubuntu18.04更改apt源為阿里云源的詳細(xì)過(guò)程_Linux
- 2022-07-29 Linux文件系統(tǒng)介紹_linux shell
- 2023-01-12 pandas中的DataFrame數(shù)據(jù)遍歷解讀_python
- 2022-10-27 教你快速搭建?React?Native?開(kāi)發(fā)環(huán)境_React
- 2022-09-06 Python詳解argparse參數(shù)模塊之命令行參數(shù)_python
- 2023-05-29 docker如何對(duì)已經(jīng)啟動(dòng)的容器添加目錄映射(掛載目錄)_docker
- 最近更新
-
- 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概述快速入門(mén)
- 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)程分支