0

Im working on large file and I want to overwrite conent file at specific position, like this:

helloworld123456

overwrite new content "internet" at 6th word

hellointernet456

Thank you so much

edit: RandomAccessFile can seek and write directly to file and its worked

randomAccessFile.seek(5); 
randomAccessFile.write("internet".getBytes());
NoOneCares
  • 13
  • 2
  • Read the file, make the change in memory, write the file – MadProgrammer Dec 03 '18 at 04:13
  • but i want write directly to the file, its very large file – NoOneCares Dec 03 '18 at 04:20
  • 1
    Unless the file is seekable, that’s not how file I/o works – MadProgrammer Dec 03 '18 at 04:24
  • If it's a "large" file, then you will need to read in parts, make what ever modifications you need and write to out to a second file, when you're done, delete the original file and rename the second file into it's place – MadProgrammer Dec 03 '18 at 04:32
  • im found the way to do this :D by using RandomAccessFile i can seek to specific position, idk how effective but its worked, thank you for the answer randomAccessFile.seek(5); randomAccessFile.write("internet".getBytes()); – NoOneCares Dec 03 '18 at 04:37

1 Answers1

0

As answered here:

You can't insert data into a file. You can overwrite data at a specific location with RandomAccessFile. However an insert requires changing all of the data after it.

how to write content in a Specific position in a File

vikk
  • 46
  • 1