欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年08月25日 09:12:59   作者:xyhwork  
這篇文章主要介紹了java 將字符串追加到文件已有內(nèi)容后面的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

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

/**
  * 將字符串追加到文件已有內(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());
   // 寫入一個(gè)換行
   fos.write("\r\n".getBytes());
      
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   if(fos != null){
    try {
     fos.flush();
     fos.close(); 
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }

補(bǔ)充知識(shí):java寫文件時(shí)往末尾追加文件(而不是覆蓋原文件),的兩種方法總結(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 {
   // 打開一個(gè)隨機(jī)訪問文件流,按讀寫方式
   RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
   // 文件長(zhǎng)度,字節(jié)數(shù)
   long fileLength = randomFile.length();
   //將寫文件指針移到文件尾。在該位置發(fā)生下一個(gè)讀取或?qū)懭氩僮鳌?
   randomFile.seek(fileLength);
   //按字節(jié)序列將該字符串寫入該文件。
   randomFile.writeBytes(content);
   //關(guān)閉此隨機(jī)訪問文件流并釋放與該流關(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è)寫文件器,構(gòu)造函數(shù)中的第二個(gè)參數(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控制臺(tái)輸出結(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)容后面的操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java驗(yàn)證電話號(hào)碼的方法

    java驗(yàn)證電話號(hào)碼的方法

    這篇文章主要介紹了java驗(yàn)證電話號(hào)碼的方法,需要的朋友可以參考下
    2014-02-02
  • 如何解決executors線程池創(chuàng)建的線程不釋放的問題

    如何解決executors線程池創(chuàng)建的線程不釋放的問題

    這篇文章主要介紹了如何解決executors線程池創(chuàng)建的線程不釋放的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • SpringMVC用XML方式實(shí)現(xiàn)AOP的方法示例

    SpringMVC用XML方式實(shí)現(xiàn)AOP的方法示例

    這篇文章主要介紹了SpringMVC用XML方式實(shí)現(xiàn)AOP的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Java 堆排序?qū)嵗?大頂堆、小頂堆)

    Java 堆排序?qū)嵗?大頂堆、小頂堆)

    下面小編就為大家分享一篇Java 堆排序?qū)嵗?大頂堆、小頂堆),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車剖析

    java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車剖析

    這篇文章主要為大家介紹了java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車的示例剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Java調(diào)度線程池ScheduledThreadPoolExecutor不執(zhí)行問題分析

    Java調(diào)度線程池ScheduledThreadPoolExecutor不執(zhí)行問題分析

    最近項(xiàng)目上反饋某個(gè)重要的定時(shí)任務(wù)突然不執(zhí)行了,很頭疼,開發(fā)環(huán)境和測(cè)試環(huán)境都沒有出現(xiàn)過這個(gè)問題。定時(shí)任務(wù)采用的是ScheduledThreadPoolExecutor,后來一看代碼發(fā)現(xiàn)踩了一個(gè)大坑。本文就來和大家聊聊這次的踩坑記錄與解決方法,需要的可以參考一下
    2023-03-03
  • 關(guān)于SpringSecurity簡(jiǎn)介以及和Shiro的區(qū)別

    關(guān)于SpringSecurity簡(jiǎn)介以及和Shiro的區(qū)別

    這篇文章主要介紹了關(guān)于SpringSecurity簡(jiǎn)介以及和Shiro的區(qū)別,在Java應(yīng)用安全領(lǐng)域,Spring Security會(huì)成為被首先推崇的解決方案,就像我們看到服務(wù)器就會(huì)聯(lián)想到Linux一樣順理成章,需要的朋友可以參考下
    2023-07-07
  • 老生常談反射之Class類的使用(必看篇)

    老生常談反射之Class類的使用(必看篇)

    下面小編就為大家?guī)б黄仙U劮瓷渲瓹lass類的使用(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • java中使用session監(jiān)聽實(shí)現(xiàn)同帳號(hào)登錄限制、登錄人數(shù)限制

    java中使用session監(jiān)聽實(shí)現(xiàn)同帳號(hào)登錄限制、登錄人數(shù)限制

    本文主要介紹了java中使用session監(jiān)聽實(shí)現(xiàn)同帳號(hào)登錄限制、登錄人數(shù)限制,通過session來監(jiān)聽在線人數(shù)和登陸限制,有需要的童鞋可以了解一下。
    2016-10-10
  • java實(shí)現(xiàn)24點(diǎn)紙牌游戲

    java實(shí)現(xiàn)24點(diǎn)紙牌游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)24點(diǎn)紙牌游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03

最新評(píng)論