導航:首頁 > 編程語言 > java追加寫入文件

java追加寫入文件

發布時間:2022-12-10 04:34:23

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文件讀寫,在一個已經有內容的文件中,追加第一行,如何做到

我的想法是可以用RandomAccessFile,這個類的seek方法,想在文件的哪個位置插入內容都行。所以你的第一行就不在話下了。但是,這個會覆蓋你文件中插入位置後面的內容。相當於我們在輸入的時候,按了鍵盤的insert鍵盤。所以,像你這種情況只能用臨時文件來存儲原有的內容,然後把要插入的數據寫入文件,再把臨時文件的內容追加到文件中。
void insert(String filename,int pos,String insertContent){//pos是插入的位置
File tmp = File.createTempFile("tmp",null);
tmp.deleteOnExit();
try{
RandomAccessFile raf = new RandomAccessFile(filename,"rw");
FileOutputStream tmpOut = new FileOutputStream(tmp);
FileInputStream tmpIn = new FileInputStream(tmp);
raf.seek(pos);//首先的話是0
byte[] buf = new byte[64];
int hasRead = 0;
while((hasRead = raf.read(buf))>0){
//把原有內容讀入臨時文件
tmpOut.write(buf,0,hasRead);

}
raf.seek(pos);
raf.write(insertContent.getBytes());
//追加臨時文件的內容
while((hasRead = tmpIn.read(buf))>0){
raf.write(buf,0,hasRead);
}
}
}

③ 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把控制台輸入的文本追加到文本文件中

/**
* 從控制台接收用戶輸入的數據,並存儲在磁碟上
* @param filePath
*/
static void userPrint(String filePath){
BufferedReader br = null;
BufferedWriter bw = null;
try{
//通過System.in返回一個InputStream對象用於構造一個InputStreamReader對象
//再用來構造一個Buffered對象
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new FileWriter(filePath,true)); //true表示是否追加
String str = br.readLine(); //接收用戶輸入
while(!str.equals("exit")){ //如果用戶輸入exit則退出循環
bw.write(str); //將用戶輸入的字元串寫入文件
bw.newLine(); //換行
bw.flush(); //刷新緩沖區,將緩沖區的字元寫入磁碟!
str = br.readLine(); //繼續接收輸入
}
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(e.getMessage());
}
finally{
try {
bw.close(); //關閉對象前會調用bw.flush();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

⑤ java 如何向txt文件中的某一行繼續寫入

Java的RandomAccessFile提供對文件的讀寫功能,與普通的輸入輸出流不一樣的是RamdomAccessFile可以任意的訪問文件的任何地方。這就是「Random」的意義所在。


相關API:

RandomAccessFile(String
name, String
mode)構造器,模式分為r(只讀),rw(讀寫)等

RandomAccessFile.readLine()方法實現對一整行的讀取,並重新定位操作位置

RandomAccessFile.write(byte[] b)用於位元組內容的寫入


示例如下:

RandomAccessFileraf=newRandomAccessFile("f:/1.txt","rw");
inttargetLineNum=10;
intcurrentLineNum=0;
while(raf.readLine()!=null){
if(currentLineNum==targetLineNum){//定位到目標行時結束
break;
}
currentLineNum++;
}
raf.write(" insert".getBytes());
raf.close();


⑥ java 一個文件內容寫入或追加到另一個文件和一個文件內容復制到另一個文件在方法上有什麼區別嗎

樓上的審題,人家題主問的是「文件追加寫入」和「文件復制」有沒有區別,不是問你怎麼實現文件追加復制。
我覺得吧這個得看你的兩段代碼了,其實想來是沒有區別的,復制的本質還是先讀文件,再把讀出來的東西寫到目標文件了。關鍵在於調用write()方法時追加標志append是true還是false,追加標志默認是false的,也就是不追加,直接覆蓋目標文件。

閱讀全文

與java追加寫入文件相關的資料

熱點內容
如何批量快速壓縮視頻 瀏覽:432
我的世界如何加入ice伺服器 瀏覽:873
兄弟cnc編程說明書 瀏覽:204
php閃電入門教程學習 瀏覽:152
金岳霖邏輯pdf 瀏覽:938
linuxtomcat線程 瀏覽:77
pboc長度加數據加密 瀏覽:187
英雄聯盟國際服手游怎麼下安卓 瀏覽:297
程序員的思路 瀏覽:234
只能用命令獲得的四種方塊 瀏覽:358
怎麼用命令方塊防止開創造 瀏覽:807
掃描版的pdf 瀏覽:790
編程貓怎樣做3d游戲 瀏覽:207
怎麼查找雲伺服器上的ftp 瀏覽:156
我的世界伺服器如何注冊賬號 瀏覽:934
統計英文字元python 瀏覽:424
linux信息安全 瀏覽:910
壓縮機接線柱爆 瀏覽:1001
程序員自主創業 瀏覽:585
匯編程序員待遇 瀏覽:360