CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    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

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    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.

  3. #3
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680
    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

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    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.

  5. #5
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680
    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

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    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.

  7. #7
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680
    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
  •  





Click Here to Expand Forum to Full Width

Featured