-1

I want to write particular string in a file after a string. My file has this already -

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

I need to write the String /sdcard/Docs/MyData after path :

Could anyone tell me how I could achieve this?

FAZ
  • 255
  • 1
  • 3
  • 13
  • 3
    read this: http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java – Michele Lacorte Feb 10 '16 at 11:52
  • Just use `MODE_APPEND` if there is already something in the file. – Rohit5k2 Feb 10 '16 at 11:53
  • You can also take a look at [Random Access File](http://docs.oracle.com/javase/tutorial/essential/io/rafs.html) if you have to read the `path :` string befor writing out its value. – elTomato Feb 10 '16 at 12:35

1 Answers1

0

If I understand correctly you mean to append your path at the end of your file. If so the use of a FileWriter is a good way to do it.

new FileWriter("Your path", true)

Notice that the boolean true in this case indicates that you want to append to your file, removing this altogether or using false instead would mean you want to overwrite the file.

An example for your case:

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 is some documentation if you need for android, normally there shouldn't be any big differences.

Voltboyy
  • 129
  • 1
  • 8
  • No actually my question was writing a String after a string. For example, writing "com.hi" after "path:". It should look like this path:com.hi – FAZ Feb 10 '16 at 16:58