Replace the first line of a file in android
In my android application i would like to replace my first line of a txt file with some other data. Is there any way that i can do this.
Please let me know y开发者_Python百科our valuable suggestions.
Thanks in advance :)
Due to the nature of Java IO and the disk file system, it is not directly possible to replace an arbitrary "line" of a file without taking the remnant of the file into consideration. You need to fully read and write the file.
The BufferedReader
and PrintWriter
are helpful in this. The former offers a readLine()
method and the latter offers a println()
method (which compensates the fact that the readLine()
eats the newline). Add some boolean
which you can use in the loop to determine if the first line has to be processed or not.
精彩评论