aspose file tools*
The moose likes I/O and Streams and the fly likes writing into the previous line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "writing into the previous line" Watch "writing into the previous line" New topic
Author

writing into the previous line

Anandh Ramesh
Ranch Hand

Joined: Dec 15, 2004
Posts: 61
hi,

i have a problem in file handling. i have to read a file for a text pattern, and whenever it is encountered, i have to insert a line of text before it, and a line of text after the pattern. how do i go about it?


cheers,<br />Anandh
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

You read one file and write a second file. When you read text that doesn't match the pattern, you copy it unchanged to the second file. When you do match the pattern, you write the data you describe in your message. Finally, you rename the original file, rename the new file to the original name, and delete the original file.


[Jess in Action][AskingGoodQuestions]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
What if....



This approach may need some kind of synchronization in a multithreaded environment.
 
Consider Paul's rocket mass heater.
 
subject: writing into the previous line