java向文件中追加內(nèi)容與讀寫文件內(nèi)容源碼實例代碼
java向文件中追加內(nèi)容與讀寫文件內(nèi)容源碼實例代碼
向文件尾加入內(nèi)容有多種方法,常見的方法有兩種:
RandomAccessFile類可以實現(xiàn)隨機訪問文件的功能,可以以讀寫方式打開文件夾的輸出流
public void seek(long pos)可以將讀寫指針移到文件尾,參數(shù)Pos表示從文件開頭以字節(jié)為單位測量的偏移位置,在該位置文件指針。
public void write(int pos)將數(shù)據(jù)寫到讀寫指針后面,完成文件的追加。參數(shù)pos表示要寫入的Byte
通過FileWrite打開文件輸出流,構(gòu)造FileWrite時指定寫入模式,是一個布爾量,為真時表示寫入的內(nèi)容添加到已有文件的內(nèi)容的后面,為假時表示重新寫文件,以前的記錄被清空,默認(rèn)的值為假。
具體的例子可以參看以下的代碼:
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實現(xiàn)文件的追加,其中:fileName表示文件名;content表示要追加的內(nèi)容
public static void appendMethod_one(String fileName, String content) {
try {
// 按讀寫方式創(chuàng)建一個隨機訪問文件流
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
long fileLength = raf.length();// 獲取文件的長度即字節(jié)數(shù)
// 將寫文件指針移到文件尾。
raf.seek(fileLength);
// 按字節(jié)的形式將內(nèi)容寫到隨機訪問文件流中
raf.writeBytes(content);
// 關(guān)閉流
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 使用FileWriter實現(xiàn)文件的追加,其中:fileName表示文件名;content表示要追加的內(nèi)容
public static void appendMethod_two(String fileName, String content) {
try {
// 創(chuàng)建一個FileWriter對象,其中boolean型參數(shù)則表示是否以追加形式寫文件
FileWriter fw = new FileWriter(fileName, true);
// 追加內(nèi)容
fw.write(content);
// 關(guān)閉文件輸出流
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("以行為單位讀取文件內(nèi)容,一次讀一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次讀入一行,直到讀入null為文件結(jié)束
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 + "文件的內(nèi)容如下:");
CharactersDemo_03.showFileContent(fileName); // 顯示文件內(nèi)容
// 按RandomAccessFile的形式追加文件
System.out.println("\n按RandomAccessFile的形式追加文件后的內(nèi)容如下:");
CharactersDemo_03.appendMethod_one(fileName, content);
CharactersDemo_03.appendMethod_one(fileName, "\n Game is Over! \n");
CharactersDemo_03.showFileContent(fileName); // 顯示文件內(nèi)容
// 按FileWriter的形式追加文件
System.out.println("\n按FileWriter的形式追加文件后的內(nèi)容如下:");
CharactersDemo_03.appendMethod_two(fileName, content);
CharactersDemo_03.appendMethod_two(fileName, "\n Game is Over! \n");
CharactersDemo_03.showFileContent(fileName); // 顯示文件內(nèi)容
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
SpringBoot + Spring Security 基本使用及個性化登錄配置詳解
這篇文章主要介紹了SpringBoot + Spring Security 基本使用及個性化登錄配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Springboot通過請求頭獲取當(dāng)前用戶信息方法詳細(xì)示范
這篇文章主要介紹了Springboot通過請求頭獲取當(dāng)前用戶信息的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11
Java項目開發(fā)中實現(xiàn)分頁的三種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于Java項目開發(fā)中實現(xiàn)分頁的三種方式,通過這一篇文章可以很快的學(xué)會java分頁功能,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
spring-security關(guān)閉登錄框的實現(xiàn)示例
這篇文章主要介紹了spring-security關(guān)閉登錄框的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
簡單了解java中靜態(tài)初始化塊的執(zhí)行順序
這篇文章主要介紹了簡單了解java中靜態(tài)初始化塊的執(zhí)行順序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
詳解Spring Cloud Gateway基于服務(wù)發(fā)現(xiàn)的默認(rèn)路由規(guī)則
這篇文章主要介紹了詳解Spring Cloud Gateway基于服務(wù)發(fā)現(xiàn)的默認(rèn)路由規(guī)則,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05

