當前位置:首頁 » 編程語言 » 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();
}
}
}

熱點內容
c語言實現數據結構的演算法 發布:2025-03-21 14:35:55 瀏覽:414
androidphp最佳實踐pdf 發布:2025-03-21 14:33:44 瀏覽:728
哪裡下安卓版60秒 發布:2025-03-21 14:32:06 瀏覽:291
javarsa分段加密 發布:2025-03-21 14:31:57 瀏覽:511
中國式家長怎麼換伺服器 發布:2025-03-21 14:21:58 瀏覽:846
腳本守約 發布:2025-03-21 14:20:55 瀏覽:102
安卓手機清理存儲空間會怎麼樣 發布:2025-03-21 14:20:17 瀏覽:25
平板怎麼給照片加密 發布:2025-03-21 14:20:12 瀏覽:1001
安卓換手機傳照片用什麼最快 發布:2025-03-21 14:17:56 瀏覽:792
學數控編程大概多少錢 發布:2025-03-21 14:09:28 瀏覽:759