File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

writing into the previous line

Anandh Ramesh
Ranch Hand

Joined: Dec 15, 2004
Posts: 61

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

Joined: Jul 08, 2003
Posts: 24199

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.
I agree. Here's the link:
subject: writing into the previous line
jQuery in Action, 3rd edition