Ⅰ 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 如何向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將文字信息追加到指定txt文件
Stringname=""//你的錄入信息
Filefile=newFile("c:/name.txt");//錄入文件地址
if(!file.exists()){
try{
file.createNewFile();
}catch(IOExceptione){
e.printStackTrace();
}
}
OutputStreamos=null;
try{
os=newFileOutputStream(file,true);//false覆蓋true追加
byte[]b=name.getBytes();
os.write(b);//寫入
os.close();//關閉流
}catch(Exceptione){
e.printStackTrace();
}
Ⅳ java 一個文件內容寫入或追加到另一個文件和一個文件內容復制到另一個文件在方法上有什麼區別嗎
樓上的審題,人家題主問的是「文件追加寫入」和「文件復制」有沒有區別,不是問你怎麼實現文件追加復制。
我覺得吧這個得看你的兩段代碼了,其實想來是沒有區別的,復制的本質還是先讀文件,再把讀出來的東西寫到目標文件了。關鍵在於調用write()方法時追加標志append是true還是false,追加標志默認是false的,也就是不追加,直接覆蓋目標文件。
Ⅳ java:往文件中寫數據,新寫入的數據總是覆蓋原有數據,怎麼能實現追加功能呢
File file=new File("f:/a.txt");
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new FileWriter(file,true));
bw.write("efg");
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
這里關鍵代碼bw=new BufferedWriter(new FileWriter(file,true));
後面參數的true,就代表即使a.txt裡面有內容,也不會替換。
Ⅵ 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語言對文件進行追加內容
額 那個java的輸入到文件的方法很多拿個例子來說一下
比如java 的輸入有個FileOutputStream 文件輸出流 實現向文件中寫入內容
他除了有個無參的構造方法還有一個構造方法
FileOutputStream(File file ,boolean append) 若第二個參數為true,當前輸入流會在文件的末尾開始寫入新的內容即追加新內容
Ⅷ 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怎樣往一個文件里多次寫入數據
所以你寫csv表格,這么做就可以了,每寫一列就加一個,就是第二列。
距離有個bufferwriter
writer對象要寫一個csv文件。
writer.wrtie("第一列");
writer.write(",");
writer.write("第二列");
這就是一個簡單的用,分割的csv文件。