Click to See Complete Forum and Search --> : Method to write to files


Bnt
February 4th, 2003, 10:05 AM
Hi All,

I need to write an app that reads from one file and writes the data read from that file to multiple files. My first idea was to open an InputStream read a byte and the write the byte to the, say, 3 OutputStreams. This approach seems a little messy becuase it messes around with Exception Handling a bit and I have been thinking of another way to do it.

The next idea I had was to write a method that accepts an InputStream as a param and then to call that method for as many files as I need to write to passing them all the same InputStream. The problem is that only the first file gets to read the data, because by the time the next file starts reading from the InputStream it is already quite far down the data line. Any ideas on how I can overcome or get passed this.

thanks again guys
Byron

dlorde
February 4th, 2003, 03:46 PM
Why don't you just create a separate InputStream for each OutputStream? This way you can organise the output sequence how you like (even run three concurrent threads, if you want).

Had and Had-had handed in their mutually recursive essays. Where Had had had "Had-had had had 'Had had had Had-had'", Had-had had had "Had had had 'Had-had had had Had'". Had Had-had had "Had-had had had 'Had had had Had-Had'", things would have been very different...

Bnt
February 6th, 2003, 01:34 AM
Thanks again dlorde, you always manage to make things seem so trivial ;-),

Bur I have another question for you, I wrote the method to accept the inputFile and the outputFile and then write the characters out using a FileWriter wrapped in a BufferedWriter, (I did this because I wanted to use the newLine() method of BufferedWriter). The problem that comes up is that if I pass append = true to the FileWriter, well, it just doesn't do that.
This is how I did it:

BufferedWriter bufWriter = new BufferedWriter(new FileWriter(outFile, true));

But this just doesn't append to files, should it?

Byron

dlorde
February 6th, 2003, 06:44 PM
But this just doesn't append to files, should it?Yes, it should append to the file. Perhaps if you post your code we can see what's going wrong.

Really, if the lower orders don't set us a good example, what on earth is the use of them?
Oscar Wilde

Bnt
February 7th, 2003, 01:20 AM
Hi dlorde,

I have found another way to acheive the result using SequenceInputStream to concatenate an array of files that I came across in the Java tutorial.

But for intereset here is the code I was using to try append to files.


public void writeToFile(File inFile, File outFile) throws Exception
{
BufferedReader bufReader = new BufferedReader(new FileReader(inFile));
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(outFile, true));

int readByte = 0;

while ((readByte = bufReader.read()) != -1)
{
bufWriter.write(readByte();
}

bufReader.close();
bufWriter.close();
}


Thanks again for your help, it is much appreciated
Byron

dlorde
February 7th, 2003, 05:01 AM
That code looks OK - in fact, I'd be inclined to call the method 'appendToFile(...)' ;)

Unless something is happening to the output file before it reaches this method, I can't explain it.

Who says computers are predictable, logical and deterministic? My PC is irritable, unreliable, and holds a grudge for weeks...

Bnt
February 10th, 2003, 02:32 AM
Thanks again for your help dlorde, it's been great getting ideas from you again

Byron