導航:首頁 > 文件目錄 > java向文件追加內容

java向文件追加內容

發布時間:2022-09-14 18:10:31

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();
}
}
}

㈡ java文件讀寫 在一個已經有內容的文件中追加第一行 如何做到

如果要在已有內容的文件開頭插入內容就只能先把原內容讀出來,然後先寫入要插入的東西,然後再把原內容寫回去,然後關閉輸出流

如果要在已有內容的文件末尾追加內容就比較簡單了

在創建FileOutputStream對象的時候第二個參數給個true,就表示續寫文件,而非覆蓋文件已有內容了

newFileOutputStream(file,true)

㈢ java 怎樣向一個已存在的文件中添加內容

如果想向某個文件最後添加內容,可使用FileWriter fw = new FileWriter("log.txt",true);在創建FileWriter時加個true就可以了。

下面是詳細的示例代碼

Filefile=newFile("D:/Test.txt");
Filedest=newFile("D:/new.txt");
try{
BufferedReaderreader=newBufferedReader(newFileReader(file));
BufferedWriterwriter=newBufferedWriter(newFileWriter(dest,true));
Stringline=reader.readLine();
while(line!=null){
writer.write(line);
line=reader.readLine();
}
writer.flush();
reader.close();
writer.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}

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

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

㈤ java把控制台輸入的文本追加到文本文件中

用io流,不要new新的File,控制台輸入的文本讀到裡面不就ok啦?

㈥ 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();
}
}
}

㈦ 對一個txt文件追加一段內容,用java實現

import java.io.File;import java.io.FileWriter;import java.io.Writer;public class Test{ public static void main(String args[]) throws Exception{ File f = new File("d:"+File.separator+"test.txt"); Writer out = null; out = new FileWriter(f,true) //true表示追內加 容 String str = "\r\n你好\r\nHello World!"; out.writer(str); out.close(); }}

㈧ 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中FileOutputStream流,向文件中追加內容,而不是覆蓋掉文件中原有的數據

public
FileOutputStream(String
name,
boolean
append)
throws
FileNotFoundException創建一個向具有指定
name
的文件中寫入數據的輸出文版件流。如果第權二個參數為
true,則將位元組寫入文件末尾處,而不是寫入文件開始處。
如上文檔,new
的時候加一個true參數則是追加。默認為false。

閱讀全文

與java向文件追加內容相關的資料

熱點內容
三級老電影 瀏覽:756
商標注冊時身份證明文件是什麼 瀏覽:726
下載什麼軟體能檢測網路密碼 瀏覽:322
內情兩個女演是誰 瀏覽:761
韓劇電影愛情免費觀看 瀏覽:562
360手機助手不支持大文件 瀏覽:621
手機打開cad文件顯示不全 瀏覽:697
突然不能刪除文件 瀏覽:712
msp430單片機程序設計 瀏覽:22
生成jvmdump文件 瀏覽:30
怎樣恢復刪除文件夾裡面的視頻 瀏覽:782
網易雲掃描音樂文件 瀏覽:717
win7隱藏文件夾顯示不出來 瀏覽:542
如何卸載淘寶網站 瀏覽:14
pscs6破解版教程 瀏覽:309
寬頻網路提速了怎麼弄 瀏覽:658
漲金幣app弄不見了怎麼找回來 瀏覽:666
大數據與導論的答案 瀏覽:579
在線觀看小電影網址 瀏覽:507
win10注冊打開chm文件 瀏覽:314

友情鏈接