Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

According to its javadoc:

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

See also: http://stackoverflow.com/questions/11371154/outputstreamwriter-vs-filewriterhttps://stackoverflow.com/questions/11371154/outputstreamwriter-vs-filewriter

You could also use a StringBuilder instead of doing manually a string concatenation.

Also, the whole method is sychronized. The message probably does not need to be constructed in the sychronized block. Only concrete write to the Stream/File should be sychronized:

long logtime = System.currentTimeMillis();
// construct string etc

sychronized(this){
    out.write(message.getBytes(StandardCharsets.UTF_8));
}

You could also try to read the code of "real" logging frameworks to get ideas how they work.

According to its javadoc:

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

See also: http://stackoverflow.com/questions/11371154/outputstreamwriter-vs-filewriter

You could also use a StringBuilder instead of doing manually a string concatenation.

Also, the whole method is sychronized. The message probably does not need to be constructed in the sychronized block. Only concrete write to the Stream/File should be sychronized:

long logtime = System.currentTimeMillis();
// construct string etc

sychronized(this){
    out.write(message.getBytes(StandardCharsets.UTF_8));
}

You could also try to read the code of "real" logging frameworks to get ideas how they work.

According to its javadoc:

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

See also: https://stackoverflow.com/questions/11371154/outputstreamwriter-vs-filewriter

You could also use a StringBuilder instead of doing manually a string concatenation.

Also, the whole method is sychronized. The message probably does not need to be constructed in the sychronized block. Only concrete write to the Stream/File should be sychronized:

long logtime = System.currentTimeMillis();
// construct string etc

sychronized(this){
    out.write(message.getBytes(StandardCharsets.UTF_8));
}

You could also try to read the code of "real" logging frameworks to get ideas how they work.

Source Link
user140547
  • 469
  • 3
  • 8

According to its javadoc:

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

See also: http://stackoverflow.com/questions/11371154/outputstreamwriter-vs-filewriter

You could also use a StringBuilder instead of doing manually a string concatenation.

Also, the whole method is sychronized. The message probably does not need to be constructed in the sychronized block. Only concrete write to the Stream/File should be sychronized:

long logtime = System.currentTimeMillis();
// construct string etc

sychronized(this){
    out.write(message.getBytes(StandardCharsets.UTF_8));
}

You could also try to read the code of "real" logging frameworks to get ideas how they work.