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 FileReader and FileWriter 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 "FileReader and FileWriter" Watch "FileReader and FileWriter" New topic

FileReader and FileWriter

divya madala
Ranch Hand

Joined: Jan 10, 2001
Posts: 61
The below code doesn't work. Is there anyway how I can read only a particular line in a file and rewrite only that line?

[ May 03, 2004: Message edited by: divya madala ]
[ May 03, 2004: Message edited by: divya madala ]
[ May 03, 2004: Message edited by: divya madala ]
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

[How To Ask Questions On JavaRanch]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
RandomAcessFile will read or write content in the middle of a file, but it can only replace content of exactly the same length. If you try to replace "hello" with "goodbye" you'll clobber the next couple bytes of whatever followed. You cannot insert or delete content in the middle of a file ... it's just a limitation of the file system. (Some operating systems understand line-oriented files and support such actions.) You're pretty much stuck with reading the whole file, making changes and writing a new file. Such is life.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: FileReader and FileWriter
It's not a secret anymore!