Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Could anyone help me with a problem

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Could anyone help me with a problem i have. I'm trying to read from a file take the info from it and append it to a new file.

Reading from the file is no problem.

My code:
<hr>
<br>
File temp1 = new File (&quot;E:/dog/cat/jimmy.txt&quot;);

File result = new File (&quot;E:/dog/result.txt&quot;);

RandomAccessFile r =
new RandomAccessFile( temp1, &quot;r&quot; );

RandomAccessFile w =
new RandomAccessFile( result, &quot;w&quot; );

StringBuffer buf = new StringBuffer();

String text;
while( ( text = r.readLine() ) != null )
buf.append( text + &quot;\n&quot; );


How do i write what i have in my StringBuffer to my file result.txt?


Any help would be great,
Cheers

Louise
 
Unless you plan on doing something with it, don't even bother buffering the data in a string buffer, just read it and write it directly. Also, for strict file copying like you are doing, you don't need to use RandomAccessFile, just use FileReader, FileWriter or FileInputStream, FileOutputStream.

int c;
while((c = r.read()) != -1)
w.write(c);
r.close();
w.close();

If you insist on doing it your way, once you have filled the StringBuffer, you can write the data using:

w.writeChars(buf.toString());

or

w.write(buf.toString().getBytes());

Cheers,

Charles

 
Cheers i used the following to write and thats great

w.write(buf.toString().getBytes());




The only thing is this does not append to the file. What ever i have written in the file before is overwritten. Is there a certain append feature. I want to add one file to another. Then i want to append some words at the end plus a line like &quot;\n**********************\n&quot; to seperate the new info. Then i read from another file and append that to my file result.
I will be reading from a group of files and want to put what is in them into one file.


Cheers

Louise
 
Hi Louise,

You can use seek(long pos) and length() -methods that can be found in RandomAccessFile:

RandomAccessFile rw = new RandomAccessFile(&quot;file.txt&quot;, &quot;rw&quot;);
rw.seek(rw.length());
// No you can write to the end of the file

-Vepo
 
You could also use FileWriter which constructor allows to define whether the data will be written to the end of file.
And if you don't want define offset and length of the data to be written you can wrap FileWriter to PrintWriter;
PrintWriter pw = new PrintWriter(new FileWriter(&quot;f.txt&quot;, true));
and you get easy-to-use println -method

For more information checkout the java API at

...I hope that helped you
 
The example below is a little similar to what you probably want, change it around a little bit and maybe i can be useful. what it does is that it takes the data of two files and puts them into one file.

import java.io.*;


public class FileAppend {

public void fileAppend() throws IOException {

BufferedReader file1 = new BufferedReader(new FileReader(&quot;C:/hello.txt&quot;));
BufferedReader file2 = new BufferedReader(new FileReader(&quot;C:/world.txt&quot;));
PrintWriter file3 = new PrintWriter(new FileOutputStream(&quot;C:/hellworld1.txt&quot;));

String s1 = new String();
String s2 = new String();
while ((s1 = file1.readLine()) != null)
{

file3.println(s1);
file3.flush();

}
while ((s2 = file2.readLine()) != null)
{
file3.println(s2);
file3.flush();
}



}

} Java Rocks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top