Code example for PrintStream

Methods: flush, print

0
   * Writes a line to the log file. 
   */ 
  private synchronized void writeLine(long absoluteTimeNanos, String sensor, String value) {
    writeTimestamp(absoluteTimeNanos);
    writeSensorType(sensor);
    out.print(value);
    finishLogLine(); 
  } 
 
  /** 
   * Writes the timestamp prefix and separator to the log file. 
   */ 
  private void writeTimestamp(long absoluteTimeNanos) {
    if (overrideTimestamp != -1) {
      absoluteTimeNanos = overrideTimestamp;  // this only happens during tests
    } 
 
    out.print(absoluteTimeNanos);
    out.print(";");
  } 
   
  /** 
   * Writes the sensor name type and separator to the log file. 
   */ 
  private void writeSensorType(String sensorType) {
    out.print(sensorType);
    out.print(";");
  } 
   
  /** 
   * Writes the sensor type, sensor name and separator to the log file.  
   */ 
  private void writeSensorId(String sensorType, String sensorName) {
    out.print(sensorType);
    out.print("/"); 
    out.print(sensorName); 
    out.print(";");
  } 
 
  /** 
   * Writes a newline to the log file, and updates the line counters. Handles 
   * flushing. 
   */ 
  private void finishLogLine() { 
    out.print("\n");
 
    // flush the buffer every 100 lines 
    lineCount++;
    if ((lineCount % 100) == 0) {
      out.flush();
    } 
  } 
 
  /** 
   * Returns the text we should write to the file for a given sensor ID.