• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with writing to a file  RSS feed

 
Akira Reddy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
    I would like to write to a file after a set of characters. For example, if I want to write "xyz" into a File "test.txt" which contains string "hello world". I want write after the letter w. The output should be "hello wxyzorld" in the file. How do it do it? Using a FileWriter and BufferedWriter I can write to a file but not at a certain position. Could you help me if there is anyway i can do it?

Thanks,
Aravind
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Off the top of my head I can think I two approaches.

1) Read contents into memory, manipulate content to insert new text, over write original file with modified content. You might run into memory issues if the file content is too large
2) Use FileChannel
From the API docs
Bytes may be read or written at an absolute position in a file in a way that does not affect the channel's current position.

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are describing an insertion into a file which requires all the bytes after the "w" to be rewritten to new locations after the "xyz" is written.
If you want to write "xyz" after "w" and get "hello wxyzd" the RandomAccessFile class has methods that would do it.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akira Reddy wrote:. . .  if I want to write "xyz" into a File "test.txt" which contains string "hello world". I want write after the letter w. The output should be "hello wxyzorld" in the file. . . .
Why? That sounds a strange requirement. What if you are writing “yxz” into a file containing, “Akira Reddy”? Which “w” are you going to put “yxz” after? Have you found out anything about FileChannel which would be useful here? I presume that “Hello Wxzyd” will not be of much use to you.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:1) Read contents into memory, manipulate content to insert new text, over write original file with modified content. You might run into memory issues if the file content is too large

If memory is an issue, you can read the source file a line at a time, write to a temp file, then when you're all done and closed the file handles, rename the latter as the former.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darn! I did not think of temp file. Good one Fred 
 
Akira Reddy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i could finally resolve the issue. I have followed the procedure of writing contents to another file and inserting the text wherever i need. Thank you all for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!