⑴ 怎麼 用 java語言對文件進行追加內容
額 那個java的輸入到文件的方法很多拿個例子來說一下
比如java 的輸入有個FileOutputStream 文件輸出流 實現向文件中寫入內容
他除了有個無參的構造方法還有一個構造方法
FileOutputStream(File file ,boolean append) 若第二個參數為true,當前輸入流會在文件的末尾開始寫入新的內容即追加新內容
⑵ java 一個文件內容寫入或追加到另一個文件和一個文件內容復制到另一個文件在方法上有什麼區別嗎
樓上的審題,人家題主問的是「文件追加寫入」和「文件復制」有沒有區別,不是問你怎麼實現文件追加復制。
我覺得吧這個得看你的兩段代碼了,其實想來是沒有區別的,復制的本質還是先讀文件,再把讀出來的東西寫到目標文件了。關鍵在於調用write()方法時追加標志append是true還是false,追加標志默認是false的,也就是不追加,直接覆蓋目標文件。
⑶ 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文件讀寫 在一個已經有內容的文件中追加第一行 如何做到
代碼如下: import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; /** * 將內容追加到文件尾部 */ public class AppendToFile { /** * A方法追加文件:使用RandomAccessFile * @param fileName 文件名 * @param content 追加的內容 */ public static void appendMethodA(String fileName, String content){ try { // 打開一個隨機訪問文件流,按讀寫方式 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); // 文件長度,位元組數 long fileLength = randomFile.length(); //將寫文件指針移到文件尾。 randomFile.seek(fileLength); randomFile.writeBytes(content); randomFile.close(); } catch (IOException e){ e.printStackTrace(); } } /** * B方法追加文件:使用FileWriter * @param fileName * @param content */ public static void appendMethodB(String fileName, String content){ try { //打開一個寫文件器,構造函數中的第二個參數true表示以追加形式寫文件 FileWriter writer = new FileWriter(fileName, true); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String fileName = "C:/temp/newTemp.txt"; String content = "new append!"; //按方法A追加文件 AppendToFile.appendMethodA(fileName, content); AppendToFile.appendMethodA(fileName, "append end. \n"); //顯示文件內容 ReadFromFile.readFileByLines(fileName); //按方法B追加文件 AppendToFile.appendMethodB(fileName, content); AppendToFile.appendMethodB(fileName, "append end. \n"); //顯示文件內容 ReadFromFile.readFileByLines(fileName); } } 希望採納
⑸ 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文件讀寫,在一個已經有內容的文件中,追加第一行,如何做到
我的想法是可以用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文件讀寫 在一個已經有內容的文件中 追加第一行 如何做到
我的想法是可以用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的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();