Java追加文件內(nèi)容的三種方法實(shí)例代碼
整理文檔,搜刮出一個(gè)Java追加文件內(nèi)容的三種方法的代碼,稍微整理精簡(jiǎn)一下做下分享。
import Java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
/**
*
* @author malik
* @version 2011-3-10 下午10:49:41
*/
public class AppendFile {
public static void method1(String file, String conent) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
out.write(conent);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(out != null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 追加文件:使用FileWriter
*
* @param fileName
* @param content
*/
public static void method2(String fileName, String content) {
FileWriter writer = null;
try {
// 打開一個(gè)寫文件器,構(gòu)造函數(shù)中的第二個(gè)參數(shù)true表示以追加形式寫文件
writer = new FileWriter(fileName, true);
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 追加文件:使用RandomAccessFile
*
* @param fileName 文件名
* @param content 追加的內(nèi)容
*/
public static void method3(String fileName, String content) {
RandomAccessFile randomFile = null;
try {
// 打開一個(gè)隨機(jī)訪問(wèn)文件流,按讀寫方式
randomFile = new RandomAccessFile(fileName, "rw");
// 文件長(zhǎng)度,字節(jié)數(shù)
long fileLength = randomFile.length();
// 將寫文件指針移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
} catch (IOException e) {
e.printStackTrace();
} finally{
if(randomFile != null){
try {
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
try{
File file = new File("d://text.txt");
if(file.createNewFile()){
System.out.println("Create file successed");
}
method1("d://text.txt", "123");
method2("d://text.txt", "123");
method3("d://text.txt", "123");
}catch(Exception e){
System.out.println(e);
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring WebSocket 404錯(cuò)誤的解決方法
這篇文章主要為大家詳細(xì)介紹了Spring WebSocket 404錯(cuò)誤的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
java并發(fā)容器CopyOnWriteArrayList實(shí)現(xiàn)原理及源碼分析
這篇文章主要為大家詳細(xì)介紹了java并發(fā)容器CopyOnWriteArrayList實(shí)現(xiàn)原理及源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Java實(shí)現(xiàn)矩陣加減乘除及轉(zhuǎn)制等運(yùn)算功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)矩陣加減乘除及轉(zhuǎn)制等運(yùn)算功能,結(jié)合實(shí)例形式總結(jié)分析了java常見(jiàn)的矩陣運(yùn)算實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01
javaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了javaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Spring Boot配置讀取實(shí)現(xiàn)方法解析
這篇文章主要介紹了Spring Boot配置讀取實(shí)現(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
String字符串拼接方法concat和+的效率對(duì)比
這篇文章主要介紹了String字符串拼接方法concat和+的效率對(duì)比,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
使用Logback設(shè)置property參數(shù)方式
這篇文章主要介紹了使用Logback設(shè)置property參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
spring profile 多環(huán)境配置管理詳解
這篇文章主要介紹了 spring profile 多環(huán)境配置管理詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01

