當前位置:首頁 » 編程語言 » java寫入追加

java寫入追加

發布時間: 2023-08-08 04:05:13

java如何追加寫入txt文件

java追加寫入txt文件代碼及注釋參考如下:

publicvoidm(){
FileWriterff=null;
try{
//查看C盤是否有a.txt文件來判定是否創建
Filef=newFile("c:\a.txt");
ff=newFileWriter(f,true);//將位元組寫入文件末尾處,相當於追加信息。
}catch(IOExceptione){
e.printStackTrace();
}
PrintWriterp=newPrintWriter(ff);
p.println("這里就可以寫入要追加的內容了");//此處為追加內容
p.flush();
ff.try{
f.flush();
p.close();
ff.close();
}catch(IOExceptione){
e.printStackTrace();
}
}

⑵ java如何追加寫入txt文件

java中,對文件進行追加內容操作的三種方法!

importjava.io.BufferedWriter;
importjava.io.FileOutputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.OutputStreamWriter;
importjava.io.PrintWriter;
importjava.io.RandomAccessFile;
//如果文件存在,則追加內容;如果文件不存在,則創建文件,追加內容的三種方法
{
@SuppressWarnings("static-access")
publicstaticvoidmain(String[]args){
AppendContentToFilea=newAppendContentToFile();
a.method1();
a.method2("E:\dd.txt","222222222222222");
a.method3("E:\dd.txt","33333333333");
}

方法1:

publicvoidmethod1(){
FileWriterfw=null;
try{
//如果文件存在,則追加內容;如果文件不存在,則創建文件
Filef=newFile("E:\dd.txt");
fw=newFileWriter(f,true);
}catch(IOExceptione){
e.printStackTrace();
}
PrintWriterpw=newPrintWriter(fw);
pw.println("追加內容");
pw.flush();
try{
fw.flush();
pw.close();
fw.close();
}catch(IOExceptione){
e.printStackTrace();
}
}

方法2:

publicstaticvoidmethod2(Stringfile,Stringconent){
BufferedWriterout=null;
try{
out=newBufferedWriter(newOutputStreamWriter(
newFileOutputStream(file,true)));
out.write(conent+" ");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
out.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}

方法3:

publicstaticvoidmethod3(StringfileName,Stringcontent){
try{
//打開一個隨機訪問文件流,按讀寫方式
RandomAccessFilerandomFile=newRandomAccessFile(fileName,"rw");
//文件長度,位元組數
longfileLength=randomFile.length();
//將寫文件指針移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content+" ");
randomFile.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}

⑶ JAVA文件追加的幾種方式

java文件追加內容的三種方法:
方法一:
public static void writeToTxtByRandomAccessFile(File file, String str){
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file,"rw");
long len = randomAccessFile.length();
randomAccessFile.seek(len);
randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法二:
public static void writeToTxtByFileWriter(File file, String content){
BufferedWriter bw = null;
try {
FileWriter fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法三:
public static void writeToTxtByOutputStream(File file, String content){
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));
bufferedOutputStream.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e ){
e.printStackTrace();
}finally{
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

熱點內容
python處理excel文件 發布:2025-02-06 16:36:09 瀏覽:439
演算法相對定位 發布:2025-02-06 16:32:42 瀏覽:725
java程序的編譯和執行 發布:2025-02-06 16:21:45 瀏覽:416
什麼是淘寶帳號和密碼 發布:2025-02-06 16:21:36 瀏覽:495
解壓前面簽 發布:2025-02-06 16:02:00 瀏覽:324
華碩訪問點 發布:2025-02-06 15:56:57 瀏覽:331
excel拼接sql 發布:2025-02-06 15:50:10 瀏覽:501
加密手機直播 發布:2025-02-06 15:49:31 瀏覽:535
自帶ftp伺服器好用嗎 發布:2025-02-06 15:26:11 瀏覽:110
win7訪問xp區域網 發布:2025-02-06 15:17:07 瀏覽:525