国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java向文件中追加內容與讀寫文件內容源碼實例代碼

2019-11-26 12:31:13
字體:
來源:轉載
供稿:網友

java向文件中追加內容與讀寫文件內容源碼實例代碼

向文件尾加入內容有多種方法,常見的方法有兩種:

RandomAccessFile類可以實現隨機訪問文件的功能,可以以讀寫方式打開文件夾的輸出流

public void seek(long pos)可以將讀寫指針移到文件尾,參數Pos表示從文件開頭以字節為單位測量的偏移位置,在該位置文件指針。

public void write(int pos)將數據寫到讀寫指針后面,完成文件的追加。參數pos表示要寫入的Byte

通過FileWrite打開文件輸出流,構造FileWrite時指定寫入模式,是一個布爾量,為真時表示寫入的內容添加到已有文件的內容的后面,為假時表示重新寫文件,以前的記錄被清空,默認的值為假。

具體的例子可以參看以下的代碼:

package Characters;import Java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.RandomAccessFile;public class CharactersDemo_03 { // 使用RandomAccessFile實現文件的追加,其中:fileName表示文件名;content表示要追加的內容 public static void appendMethod_one(String fileName, String content) { try {  // 按讀寫方式創建一個隨機訪問文件流  RandomAccessFile raf = new RandomAccessFile(fileName, "rw");  long fileLength = raf.length();// 獲取文件的長度即字節數  // 將寫文件指針移到文件尾。  raf.seek(fileLength);  // 按字節的形式將內容寫到隨機訪問文件流中  raf.writeBytes(content);  // 關閉流  raf.close(); } catch (IOException e) {  e.printStackTrace(); } } // 使用FileWriter實現文件的追加,其中:fileName表示文件名;content表示要追加的內容 public static void appendMethod_two(String fileName, String content) { try {  // 創建一個FileWriter對象,其中boolean型參數則表示是否以追加形式寫文件  FileWriter fw = new FileWriter(fileName, true);  // 追加內容  fw.write(content);  // 關閉文件輸出流  fw.close(); } catch (IOException e) {  e.printStackTrace(); } } public static void showFileContent(String fileName) { File file = new File(fileName); BufferedReader reader = null; try {  System.out.println("以行為單位讀取文件內容,一次讀一整行:");  reader = new BufferedReader(new FileReader(file));  String tempString = null;  int line = 1;  // 一次讀入一行,直到讀入null為文件結束  while ((tempString = reader.readLine()) != null) {  // 顯示行號  System.out.println(line + ": " + tempString);  line++;  }  reader.close(); } catch (IOException e) {  e.printStackTrace(); } finally {  if (reader != null) {  try {   reader.close();  } catch (IOException e1) {  }  } } } public static void main(String[] args) { String fileName = "C:/temp/append.txt"; String content = "Successful operation!"; System.out.println(fileName + "文件的內容如下:"); CharactersDemo_03.showFileContent(fileName); // 顯示文件內容 // 按RandomAccessFile的形式追加文件 System.out.println("/n按RandomAccessFile的形式追加文件后的內容如下:"); CharactersDemo_03.appendMethod_one(fileName, content); CharactersDemo_03.appendMethod_one(fileName, "/n Game is Over! /n"); CharactersDemo_03.showFileContent(fileName); // 顯示文件內容 // 按FileWriter的形式追加文件 System.out.println("/n按FileWriter的形式追加文件后的內容如下:"); CharactersDemo_03.appendMethod_two(fileName, content); CharactersDemo_03.appendMethod_two(fileName, "/n Game is Over! /n"); CharactersDemo_03.showFileContent(fileName); // 顯示文件內容 }}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福建省| 大姚县| 芦山县| 朝阳县| 花垣县| 浦城县| 新津县| 沾益县| 皮山县| 灵山县| 麻江县| 达孜县| 芦溪县| 苏尼特左旗| 营山县| 会理县| 翁牛特旗| 康保县| 德惠市| 陆良县| 琼中| 霍邱县| 南岸区| 措勤县| 平罗县| 张掖市| SHOW| 涞水县| 康乐县| 新丰县| 山阳县| 营山县| 宝山区| 武邑县| 额尔古纳市| 武隆县| 江川县| 眉山市| 百色市| 大宁县| 琼结县|