Kirk B
Kirk B

Reputation: 571

outputStream.println() - not printing to .log file?? I can't see why

I'm trying to print a time entry to a log file and it will not output. I have used almost the same exact code to log messages and it worked fine. Can anyone see something I'm not seeing? The testing print lines all print, and no errors occur. Thanks for your help...

private static String logTime(long time, String operation)
  {
    PrintWriter outputStream = null;
    String logFile = "time.log";

    System.out.println("Writing output");
    try{
    System.out.println("Writing output2");
      outputStream = new PrintWriter(new FileOutputStream(logFile, true));


    } catch(FileNotFoundException e){
      System.out.println("File not found by logTime.");
    }
    System.out.println("Writing output3");

    outputStream.println(operation); //operation + " : " + Long.toString(time) + "Mills"
    outputStream.close();

    return "";
  }

Upvotes: 1

Views: 2900

Answers (1)

wolfrevo
wolfrevo

Reputation: 7303

In your code your use the constructor without automatic line flushing.

PrintWriter(OutputStream out) Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.

You should therefore use outputStream.flush() or use the constructor with automatic line flushing:

PrintWriter(OutputStream out, boolean autoFlush).

For details see http://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html

Upvotes: 1

Related Questions