|
-
February 4th, 2003, 11:05 AM
#1
Method to write to files
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
-
February 4th, 2003, 04:46 PM
#2
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...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
February 6th, 2003, 02:34 AM
#3
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
-
February 6th, 2003, 07:44 PM
#4
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
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
February 7th, 2003, 02:20 AM
#5
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.
Code:
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
-
February 7th, 2003, 06:01 AM
#6
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...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
February 10th, 2003, 03:32 AM
#7
Thanks again for your help dlorde, it's been great getting ideas from you again
Byron
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|