Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:
JavaWorld Daily Brew

How to write partially in file.


Can anybody please tell me how to write partially in file.

What I want to know is , is there any method in writer classes of java.io package using which i can write partially in file.Or else I have to do it by myself.

Situation is , I have a file which holds some records , I save records in file using writeln() function of Printwriter class. Now if i want to delete any records or edit I have to read the whole file , delete or change the respective record and then write the whole file again in write mode by opening new file with same name.

I dont want to write the complete file, I want only to edit the records not the whole file writing?

Please Help...

Your rating: None Average: 5 (1 vote)

You can use RandomAccessFile

You can use RandomAccessFile class and there are methods that you can read/write String. With that you can open the file, read/write/insert anywhere without re-writing the entire file.

Totally agreed with this.

Totally agreed with this.

Try RandomAccessFile

Not sure if this would help (Guess it should)

Use RandomAccessFile api and not the normal FileReader/Writer api's.

http://java.sun.com/j2se/1.4.

Sounds like you need Random

Sounds like you need Random Access File operations. You have to figure out some way of knowing where exactly in the file you records begin/end. Deleting records would not be possible unless you re-write the entire file (unless the record happens to be the last one in the file, in which case you can truncate it), but you can have a record status field and mark it as deleted. At a later point you can "defrag" the file once to purge all obsolete records as one operation.

http://java.sun.com/docs/books/tutorial/essential/io/rafs.html

This link might be

Partially writing a file

As my suggestions to ur question u can use GO TO method in Java.lang package and can trace from java.io package which has FileInputStream from this u can read ur file and can edit or delete which ever informartion u want to do without any alteration of ur whole file.

RandomAccessFile is probably

RandomAccessFile is probably your best bet. Useful for large files and for when the records are a known length, so you can seek past them. Not necessarily going to be very efficient code, though.

Thanks for the suggestions -

Thanks for the suggestions - I was also having problems figuring it out as well.

Re:

Now if i want to delete any records or edit I have to read the whole file , delete or change the respective record and then write the whole file again in write mode by opening new file with same name.
Business management degree | fire school | Education degree

Re:

With that you can open the file, read/write/insert anywhere without re-writing the entire file.
Master Degree | Associate Degree

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.