• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to write data at the middle of the file ?  RSS feed

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i want to do is
i know how to write into a file, i mean writing at the starting position or appending data at the last
but how to write in the middle of two line for eg :
consider this as a file

now if i tell you to add a line say "Who are you " in between line no 1 and 3 then how would you write it ?
 
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would read the whole file into memory. Then I would make the insertion into that data in memory. Then I would write the data out to create a new version of the file. If the file was likely to be too big to fit into memory all at once, I would write a streaming version of that algorithm.

(All operating systems that you're likely to ever use model a file as an array of bytes. You can't insert data into the middle of an array of bytes.)

 
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Copy the data starting at line 3, then go to line 2, write what you want to write, and then append everything you copied.

If you have to copy a *lot*, you can temporarily store the data in a new file, and then merge the two files at the correct position once you're done making edits.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Copy the data starting at line 3, then go to line 2, write what you want to write, and then append everything you copied.

If you have to copy a *lot*, you can temporarily store the data in a new file, and then merge the two files at the correct position once you're done making edits.

but thats the whole point of asking this question because programatically how i can just copy the 3rd line ?
 
Saloon Keeper
Posts: 4036
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has no built-in provisions for that; you'll have to write it yourself in one of the ways mentioned above.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naved momin wrote:
Stephan van Hulst wrote:Copy the data starting at line 3, then go to line 2, write what you want to write, and then append everything you copied.

If you have to copy a *lot*, you can temporarily store the data in a new file, and then merge the two files at the correct position once you're done making edits.

but thats the whole point of asking this question because programatically how i can just copy the 3rd line ?


I'm not sure what he meant by that.

I would suggest one of 2 approaches.

1. If the file is small enough, read line by line into a List in memory. Then write out to replace the original file. After you've written the first 2 lines, insert the new line, then write the rest of the lines from the list. (Or you could insert the new line into the list and then just iterate over the list, writing out each line.)

2. For a larger file, create a temp file. Read a line from the original, write it immediately to the temp, so that you have only one line at a time in memory. When you've read all the lines up to the point you want to insert something, write out that new line. Then continue the read a line/write a line loop. When you're done, delete the original and rename the new one to the original name.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
1. If the file is small enough, read line by line into a List in memory. Then write out to replace the original file. After you've written the first 2 lines, insert the new line, then write the rest of the lines from the list. (Or you could insert the new line into the list and then just iterate over the list, writing out each line.)


For fellow lazybums out there, there is a FileUtils class in one of Apache's many util libraries, which contains readLines() and writeLines() functions. You dont have to iterate!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!