java追加寫入txt文件的方法總結(jié)
java中,對(duì)文件進(jìn)行追加內(nèi)容操作的三種方法
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
//如果文件存在,則追加內(nèi)容;如果文件不存在,則創(chuàng)建文件,追加內(nèi)容的三種方法
public class AppendContentToFile {
@SuppressWarnings("static-access")
public static void main(String[] args) {
AppendContentToFile a = new AppendContentToFile();
a.method1();
a.method2("E:\\dd.txt", "222222222222222");
a.method3("E:\\dd.txt", "33333333333");
}
方法1:
public void method1() {
FileWriter fw = null;
try {
//如果文件存在,則追加內(nèi)容;如果文件不存在,則創(chuàng)建文件
File f=new File("E:\\dd.txt");
fw = new FileWriter(f, true);
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(fw);
pw.println("追加內(nèi)容");
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
方法2:
public static void method2(String file, String conent) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true)));
out.write(conent+"\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法3:
public static void method3(String fileName, String content) {
try {
// 打開一個(gè)隨機(jī)訪問文件流,按讀寫方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件長(zhǎng)度,字節(jié)數(shù)
long fileLength = randomFile.length();
// 將寫文件指針移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content+"\r\n");
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上就是腳本之家給大家整理的全部相關(guān)內(nèi)容,希望能夠幫助到大家。
相關(guān)文章
Spring使用@Filter注解創(chuàng)建自定義過濾器
Spring 中鮮為人知但非常有用的注解之一是 @Filter,它支持自定義過濾器,下面我們就來深入研究一下如何使用 Spring 的 @Filter 注解來創(chuàng)建自定義過濾器吧2023-11-11
阿里Druid數(shù)據(jù)連接池引發(fā)的線上異常解決
這篇文章主要為大家介紹了一次關(guān)于阿里Druid數(shù)據(jù)連接池引發(fā)的線上異常問題的解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
SpringMvc導(dǎo)出Excel實(shí)例代碼
本篇文章主要介紹了SpringMvc導(dǎo)出Excel實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
idea2020.3配置maven環(huán)境并配置Tomcat的詳細(xì)教程
這篇文章主要介紹了idea2020.3配置maven環(huán)境并配置Tomcat的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Springboot實(shí)現(xiàn)VNC的反向代理功能
這篇文章主要介紹了Springboot實(shí)現(xiàn)VNC的反向代理,搭建過程也很簡(jiǎn)單,通過注冊(cè)bean攔截指定URL路徑進(jìn)行自定義操作,具體實(shí)例代碼跟隨小編一起看看需要的朋友可以參考下2021-09-09
詳解jdbc實(shí)現(xiàn)對(duì)CLOB和BLOB數(shù)據(jù)類型的操作
這篇文章主要介紹了詳解jdbc實(shí)現(xiàn)對(duì)CLOB和BLOB數(shù)據(jù)類型的操作的相關(guān)資料,這里實(shí)現(xiàn)寫入操作與讀寫操作,需要的朋友可以參考下2017-08-08
Spring中的FactoryBean與BeanFactory詳細(xì)解析
這篇文章主要介紹了Spring中的FactoryBean與BeanFactory詳細(xì)解析,在Spring框架中,FactoryBean和BeanFactory是兩個(gè)關(guān)鍵的接口,用于創(chuàng)建和管理對(duì)象實(shí)例,它們?cè)赟pring的IoC(Inversion of Control,控制反轉(zhuǎn))容器中發(fā)揮著重要的作用,需要的朋友可以參考下2023-11-11
Spring中校驗(yàn)器(Validator)的深入講解
Spring校驗(yàn)器,參數(shù)校驗(yàn)從此簡(jiǎn)單。下面這篇文章主要給大家介紹了關(guān)于Spring中校驗(yàn)器(Validator)的相關(guān)資料,文中通過示例代碼介紹非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06

