2016-02-10 170 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