PrintWriter grief
Results 1 to 4 of 4

Thread: PrintWriter grief

  1. #1
    Senior Member
    Join Date
    Aug 2003
    Posts
    1,019

    Angry PrintWriter grief

    Ok, I'm dumping results to a text file via PrintWriter...

    The problem is that each line I write to my text file overwrites the previous, so all I end up with is the final result... the Java Sun forums are unfortunately not helping.

    Code:
            File logFile = new File("C:/Correlate.log");
            FileOutputStream logFileStream = new FileOutputStream (logFile);
            PrintWriter writeFile = new PrintWriter (logFileStream);
        
            
            try
            {
                 if(writeFlag == 1)
                 {  
                    //writeFile.println("\n\n"); 
                    writeFile.println(noSignature);
                    writeFile.println(Correlate.getFile() + "\r\n");
                 }
            }
            
            catch(Exception e)
            {
                System.out.println("Crap...oooops!!");
            }
            
            writeFile.close();
        }
    I've used the carriage returns...what am I missing? My other alternative is to use BufferedWriter, but for some reason that's not being recognized...

    Grrr....lol

  2. #2
    Senior Member
    Join Date
    Aug 2003
    Posts
    1,019

    Now I really have to laugh

    Just as a mindless aside, whenever I have a problem, or when I lose something... as soon as I ask, or buy a new whatever that I lost, I find it.

    So if anybody cares, in order to make the lines show up one after another, one of the lines needs to be changed....DOH!!!

    Code:
            File logFile = new File("C:/Correlate.log");
            FileOutputStream logFileStream = new FileOutputStream (logFile, true);  <--  added true
            PrintWriter writeFile = new PrintWriter (logFileStream);
    If you do it like I did in the first example, every time you try to write to the file, it creates a new file... who knew??!!!

    Now if I could just drill down through my directories like I want

  3. #3
    AntiOnline n00b
    Join Date
    Feb 2004
    Posts
    665
    hi

    The problem is that each line I write to my text file overwrites the previous,
    so all I end up with is the final result...
    what happned to use the append() method.

    Code:
     
    f = new File("myFile.txt")
    f.append("Hello World\n")
    f.append("Hello again, that's me!")

  4. #4
    Senior Member
    Join Date
    Aug 2003
    Posts
    1,019
    what happned to use the append() method.
    Actually, I didn't know one could use it that way. I'll tuck that info away for future reference though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides