Writing to a file
Hi, I am working on a program to read records from a file and write it to another file. For writing, I am using a bufferedwriter, which I think will write to the file once the buffer is full. The requirement in my application is the records are grouped based on a certain criteria. Is there any way I write records one group at a time to the file? I dont know How many records will be in any group.
Also, I would like to know how to move a file from one folder to another folder. Is there any method to do that?
Any help is appreciated.
[563 byte] By [
gopisuri] at [2007-9-26 2:03:40]

Hi,
There are several classes that can write to a file with a little more ease then BufferedWriter, although ultimately they are all doing the same thing. PrintWriter is often the most convenient class. All of these classes have flush() which will write the buffer's current content to the stream's destination, ie the file you intent to create.
There is no native/method class for copying files that I am aware of, but you can use FileInputStream and FileOutputStream to copy, just have a while loop that reads in the stream from a FileInputStream instance and then write that stream to another file via FileOutputStream, like:
while (true) {
int i = fin.read();
if (i == -1)
break;
else
out.write(i);
}
I don't if PrintWriter has a default buffer size. But, let me ask you this question. As I mentioned before, I need to write a group of records at a time. What if I am in the middle of writing a group to the buffer and the buffer gets full and writes it to the file?