• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to insert data into existing file

 
rahul thakare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have an allready written file and i want replace or insert some text into
that file. i tried lot but im unable to do that, so i am rewriting all data plus new data into new file.

plz can anybody help me to get out of this problem..
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use the constructor of FileWriter that accepts a file name and a boolean, and you send true as the boolean, then you are opening the file for appending.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know thw exact layout of the data in the file, you might be able to use the RandomAccessFile class to update parts of it.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!!

Often the simplest thing to do is read the whole file, pass most of it through unchanged, change the bits you have to change and write a new file. It sounds like that's what you're doing. I often mention this old trick from IBM's VM/CMS:

read original
write temp
rename original to backup
rename temp to original
erase backup

That always has a safe copy of either the old and/or the new in case you crash in the middle.

The random access file technique is cool but complex. Imagine a row of childrens blocks on your desk. If you want to insert one in the middle you could move the last one over one spot, then move the next to last over one spot and so on until you make an empty spot for the insert. Or you could pick up all the blocks from the insert point to the end and move them over one spot. You can do the same thing with seek, read and write. Good fun, but I'm not sure it will be a lot faster than reading and rewriting the whole thing.

Tell us how you do it!
[ March 08, 2006: Message edited by: Stan James ]
 
rahul thakare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi james

thanks...

i was doing the same what u said.
but problem is that im reading the original file which contain some
different characters, which could not get write properly in temp file
so that i want whole original file should remain same,
only required text should get changed.
plz can u find solun on this prob..

i have one idea
can we do it by increasing file length

plz reply...
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rahul thakare:

i was doing the same what u said.
but problem is that im reading the original file which contain some
different characters, which could not get write properly in temp file



That sounds like your read/write code has some problems. Perhaps you could share a small sample of code with us?
 
rahul thakare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

this how the .frx file creates when u creact VB application using
RichTextBox data in richtext box is RAHULT

� after this 3 junk block n then following

{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\fswiss MS Sans Serif;}}
{\colortbl\red0\green0\blue0;}
\deflang1033\pard\plain\f2\fs24\b RAHULT
\par }

this might be written in binary format at start the start
but ther seem to be text from {\rtf to \par}
[ March 09, 2006: Message edited by: rahul thakare ]
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like you are trying to use a Reader/Writer to work on a binary file (i.e. a MS Word document, I don't know if Foxpro/VB reports are binary or character. Sounds like they're binary). The Reader/Writer classes in Java are only for working on plain text files. If you pass binary data through them, they will attempt to map the data to a Unicode character set. If a particular set of bytes does not map to a Unicode character, you lose data. Have a look at the Java tutorial on IO for more on Readers/Writers vs. Streams.
[ March 10, 2006: Message edited by: Joe Ess ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!