Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 22-01-2010
Member
 
Join Date: Dec 2009
Posts: 292
Write to the middle of a file

Hello,
I write to a file that already contains data. No crush there after, I would write my data as a string after the 10th character in my file. This is not quite useful for me and my program. I just use methods between FileOutputStream and others. So, if you guys know any other methods then you can help me. In brief I want the write to the middle of the file. Thank you in advance.
Reply With Quote
  #2  
Old 22-01-2010
Member
 
Join Date: May 2008
Posts: 2,008
Re: Write to the middle of a file

Hi,
There's a simple solution that is a copy in another file front lines, adds new lines to write at the end of the first file. Or load all of your lines in the memory and rewrite your files from A to Z, that is the write the full file again. What is sure is that it works, but it can not be better that that, i think so. May be there are some other solutions for this, i have no idea about the others, but this works.
Reply With Quote
  #3  
Old 22-01-2010
Member
 
Join Date: May 2008
Posts: 2,293
Re: Write to the middle of a file

Hello,
You have the choise to use the method used by the above posted, but it is not very optimized and that may take some time if the file is quite large. If you normally use FileChannel class that will allow you to do that. I think you should search the maps in java and you should find one method for that. But I think the FileChannel method is an efficient choice here, that is for your program.
Reply With Quote
  #4  
Old 22-01-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Write to the middle of a file

Hello,
But I do not think mapped file are not in itself a solution. Unless something we can not escape me no more "insert" data into a file that is mapped to a file on the hard drive? The mapped file could speed the processing of large files, but apart from that the methods could be employed are no better to use or easier to implement than solutions for a file on the hard disk.
Reply With Quote
  #5  
Old 22-01-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Write to the middle of a file

Hi,
Yes I am with the above post, here are some of the alternatives to that
- pass through an intermediate file (very simple, not necessarily very fast, especially for an insertion end of file)
- read the entire file into memory (eg StringBuffer()) and then overwrite it with new data (the simplest and slowest),
- Open file read-write and use a buffer to copy the end of the file by small bits after the inserted data" (probably the fastest with a suitable buffer size, but heavy enough to put in place).
Reply With Quote
  #6  
Old 22-01-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Write to the middle of a file

Hello
Even I believe somewhat to the above post. The above posted alternative can be a good choice for you. Possibly you have the RandomAccessFile this can do the trick for you. Have you ever tried using the RandomAccessFile? If not then please try because if has many methods and they all are useful and I feel they can fit your need, for your program.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Write to the middle of a file"
Thread Thread Starter Forum Replies Last Post
Javascript write to file Din_the1 Software Development 4 29-05-2010 12:38 PM
Open a txt file and write into it Maya Angelou Software Development 5 25-03-2010 01:59 PM
Write to an XML file without overwriting TechGate Software Development 5 18-01-2010 11:11 AM
How do we write exit procedure or function in javascript to exit middle of the program? Jevin Software Development 3 22-09-2009 09:38 AM
How to Write a Batch File ? Ebenezer Operating Systems 2 30-07-2009 02:39 PM


All times are GMT +5.5. The time now is 03:28 PM.