亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

java 將字符串追加到文件已有內(nèi)容后面的操作

瀏覽:3日期:2022-08-26 10:44:19

我就廢話不多說了,大家還是直接看代碼吧~

/** * 將字符串追加到文件已有內(nèi)容后面 * * @param fileFullPath 文件完整地址:D:/test.txt * @param content 需要寫入的 */ public static void writeFile(String fileFullPath,String content) { FileOutputStream fos = null; try { //true不覆蓋已有內(nèi)容 fos = new FileOutputStream(fileFullPath, true); //寫入 fos.write(content.getBytes()); // 寫入一個換行 fos.write('rn'.getBytes());} catch (IOException e) { e.printStackTrace(); }finally{ if(fos != null){ try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }

補充知識:java寫文件時往末尾追加文件(而不是覆蓋原文件),的兩種方法總結(jié)

代碼如下:

import java.io.FileWriter;import java.io.IOException;import java.io.RandomAccessFile; public class AppendToFile { /** * A方法追加文件:使用RandomAccessFile */ public static void appendMethodA(String fileName, String content) { try { // 打開一個隨機訪問文件流,按讀寫方式 RandomAccessFile randomFile = new RandomAccessFile(fileName, 'rw'); // 文件長度,字節(jié)數(shù) long fileLength = randomFile.length(); //將寫文件指針移到文件尾。在該位置發(fā)生下一個讀取或?qū)懭氩僮鳌? randomFile.seek(fileLength); //按字節(jié)序列將該字符串寫入該文件。 randomFile.writeBytes(content); //關(guān)閉此隨機訪問文件流并釋放與該流關(guān)聯(lián)的所有系統(tǒng)資源。 randomFile.close(); } catch (IOException e) { e.printStackTrace(); } } /** * B方法追加文件:使用FileWriter */ public static void appendMethodB(String fileName, String content) { try { //打開一個寫文件器,構(gòu)造函數(shù)中的第二個參數(shù)true表示以追加形式寫文件,如果為 true,則將字節(jié)寫入文件末尾處,而不是寫入文件開始處 FileWriter writer = new FileWriter(fileName, true); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String fileName = 'C:/Temp.txt'; String content = 'new append!'; //按方法A追加文件 AppendToFile.appendMethodA(fileName, content); AppendToFile.appendMethodA(fileName, 'append end. n'); //顯示文件內(nèi)容 ReadFromFile.readFileByLines(fileName); //按方法B追加文件 AppendToFile.appendMethodB(fileName, content); AppendToFile.appendMethodB(fileName, 'append end. n'); //顯示文件內(nèi)容 ReadFromFile.readFileByLines(fileName); }}

java控制臺輸出結(jié)果如下:

++++++readFileByLines:++++++

以行為單位讀取文件內(nèi)容,一次讀一整行:

line 1: Sun Yat-sen(November 12, 1866?March 12, 1925) was a Chinese revolutionary and political leader who is often referred to as the 'father of modern China'. Sun played an instrumental and leadership role in the eventual overthrow of the Qing Dynasty in 1911. He was the first provisional president when the Republic of China was founded in 1912. He later co-founded the Kuomintang (KMT) where he served as its first leader. new append!append end.

++++++readFileByLines:++++++

以行為單位讀取文件內(nèi)容,一次讀一整行:

line 1: Sun Yat-sen(November 12, 1866?March 12, 1925) was a Chinese revolutionary and political leader who is often referred to as the 'father of modern China'. Sun played an instrumental and leadership role in the eventual overthrow of the Qing Dynasty in 1911. He was the first provisional president when the Republic of China was founded in 1912. He later co-founded the Kuomintang (KMT) where he served as its first leader. new append!append end. line 2: new append!append end.

以上這篇java 將字符串追加到文件已有內(nèi)容后面的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 真人午夜a一级毛片 | 免费直接看黄 | 性色生活免费看性大片 | 久久久久久在线 | 中国做爰国产精品视频 | 偷亚洲偷国产欧美高清 | 国产99久9在线 | 成人国内精品久久久久影 | 成人在线观看一区 | 亚洲第一页在线视频 | 91蝌蚪国产| 孕交欧美videos | 黄色好看视频 | 亚洲精品色婷婷在线影院麻豆 | 亚洲综合影院 | 国产黄在线播放免费观看 | 国产日韩在线播放 | 日本亚洲成高清一区二区三区 | 青青操手机看 | 很很射影院 | 亚洲美日韩 | 国产极品嫩模大尺度福利视频 | 黄色淫片| 亚洲成熟xxxxx电影 | 国产成人禁片免费观看 | 国产亚洲精品成人婷婷久久小说 | 国产黄色片在线播放 | 1级黄色| 色婷婷激婷婷深爱五月老司机 | 性一级视频 | 看黄a大片 免费 | 欧美午夜在线播放 | 亚洲精品二区中文字幕 | 欧美夜夜精品一级爽 | 日韩国产片| 国产黄色二级片 | 污污的网站有哪些呢 | 六月婷婷视频 | 日韩欧美亚洲一区精选 | 黄网在线免费 | 日韩在线观看视频黄 |