File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes how to change the content of a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to change the content of a file" Watch "how to change the content of a file" New topic

how to change the content of a file

mythily prakash
Ranch Hand

Joined: Jan 27, 2006
Posts: 31
I want to change the content of a file,not all the contents but some slite modifications to the file.Can anyone help me to get a code to do this?

Best Regards,<br />Mythily.S<br />9886530336
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
I think we need some more details - for example, what is the file name (or would you prefer to specify it on the command line), what is the type of file and its contents (e.g. plain text, XML, binary, compressed archive) and what sort of changes do you need to make?

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In short, you can overwrite bytes in the middle of a file, but you can't easily replace old stuff with new stuff of a different length. So change and append are easy, but insert and delete are tough. It's common to read all of the old file and write a totally new one with the same or changed content.

See RandomAccessFile if you can be sure the data length doesn't change. Otherwise just read and write with whatever IO works with your data.
[ March 16, 2006: Message edited by: Stan James ]

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: how to change the content of a file
It's not a secret anymore!