2016-02-10 168 views
-1

我想在字符串之後的文件中寫入特定的字符串。我的文件有這個已經 -將字符串寫入特定字符串後的文件

############################## 
path : 

我需要寫字符串/sdcard/Docs/MyDatapath :

誰能告訴我,我怎麼能做到這一點?

+3

閱讀此:http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java –

+0

只要使用'MODE_APPEND',如果已經有東西在文件。 – Rohit5k2

+0

如果您必須閱讀'path:'befor寫出的字符串,您還可以查看[Random Access File](http://docs.oracle.com/javase/tutorial/essential/io/rafs.html)它的價值。 – elTomato

回答

0

如果我理解正確,你的意思是追加你的文件末尾的路徑。 如果是的話,使用FileWriter是一個好辦法。

new FileWriter("Your path", true) 

請注意,在這種情況下,布爾true表示要附加到文件,完全刪除此或使用false,而不是就意味着要覆蓋該文件。

您的情況的一個例子:

try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("C:/YourEpicPath/ThisFileNeedsSomeAppending.txt", true)))) { 
    out.println("/sdcard/Docs/MyData"); 
}catch (IOException e1) { 
    //exception handling left as an exercise for the reader 
} 

Here是一些文件,如果你需要爲Android,一般不應該有任何大的差異。

+0

其實我的問題是在一個字符串之後寫一個字符串。例如,在「path:」之後寫「com.hi」。它應該看起來像這樣:com.hi – FAZ