How to change the part of content of the file, starting at specific character, without reading and writing whole file?
2 Answers
Use java.io.RandomAccessFile class. You can seek() to an arbitrary position in the file and then read or write from/to there. Try looking at writeUTF(String) for writing text, and getFilePointer() for remembering position in the file. Unfortunately, there is no easy way to "insert" text as you would do it in an editor, instead the contents are always "overwritten".
Also, FileWriter and FileOutputStream support append-mode, which you can use for appending extra data to the end of the file without rewriting it. But if you need to change things in the middle, you have to use random access file.
Comments
check out the scanner class
it makes it easier to read and parse strings and primitive types using regular expressions.