How do you write to text files WITHOUT OVERWRITING?

J

javajavalink

I am relatively new to java and have already tried many ways to write
to text files with Java. Is there any way to write to a text file
without overwriting what is already there??
 
R

Rob

javajavalink said:
I am relatively new to java and have already tried many ways to write
to text files with Java. Is there any way to write to a text file
without overwriting what is already there??
-You could append to the file, using something like:
BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));

-You could look up RandomFileAccess
-You could read the file into a String, manipulate the string
and write it back out.
 
K

klynn47

If you create a new instance of a FileReader with just the filename,
then that file will be erased if it is already there. You can use the
constructor of FileReader that accepts the filename and a boolean. If
the boolean is true, then the file will be appended.
 
K

klynn47

If you create a new instance of a FileReader with just the filename,
then that file will be erased if it is already there. You can use the
constructor of FileReader that accepts the filename and a boolean. If
the boolean is true, then the file will be appended.
 
N

Niels Dybdahl

I am relatively new to java and have already tried many ways to write
to text files with Java. Is there any way to write to a text file
without overwriting what is already there??

None of the most used filesystems can insert data at a random place without
overwriting what is already there. So all operating systems and most
programming languages work in the same way. Databases use tree structures to
make it possible to insert data in the middle without overwriting anything.

Niels Dybdahl
 
S

Storm

Niels said:
None of the most used filesystems can insert data at a random place without
overwriting what is already there. So all operating systems and most
programming languages work in the same way. Databases use tree structures to
make it possible to insert data in the middle without overwriting anything.

Niels Dybdahl

Actually yes you can insert data at random places by utilizing the
RandomFile class. By supplying the length of each record, and the end
record number, you can reset the file size to the oldsize + newRec
length and boom you have an append. Also, you can write data into the
middle of a file without overwriting. All you have to do is create a
class that represents each row, read each record into the created
class, insert the new record at the appropriate point in the vector
during a For/While loop, and re-write the file using the data present
in the vector.

Storm
 
M

Michael Borgwardt

Storm said:
Actually yes you can insert data at random places by utilizing the
RandomFile class.

What "RandomFile" class? Do you mean java.io.RandomAccessFile? Then
you're wrong.
By supplying the length of each record, and the end
record number, you can reset the file size to the oldsize + newRec
length and boom you have an append.

Nobody said anything about appending. That can be done.
Also, you can write data into the
middle of a file without overwriting.

No, you cannot.
All you have to do is create a
class that represents each row, read each record into the created
class, insert the new record at the appropriate point in the vector
during a For/While loop, and re-write the file using the data present
in the vector.

Please tell us that was a joke.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top