3

I am trying out a simple program for printing characters. When I do this:

import java.io.*;

public class listit {

  public static void main(String[] args) {

    for (int i = 32; i < 127; i++) {
      System.out.write(i);
      // break line after every eight characters.
      if (i % 8 == 7) System.out.write('\n');
      else System.out.write('\t');
    }
    System.out.write('\n');
   }
} 

I am getting the expected result, that is, the printable subset of the ASCII character set is being printed out. However, when I try something similar:

import java.io.*;
public class listit {
public static void main(String[] args) {
int i = 122;
System.out.write(i);
}
}

I am getting no output at all, while I was expecting z. How is this program different from the one above, save the absence of a loop?

SexyBeast
  • 8,635
  • 25
  • 92
  • 179

2 Answers2

6

PrintStream supports auto-flushing or flushing on a newline.

System.out has auto-flushing enabled but for System.out.write('A') it will only auto-flush if you write a newline. Note: if you do System.out.write("A".getByte()) will auto-flush.

The Javadoc for PrintStream.write(int) states

Writes the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.

This means you need auto-flush AND write a newline.

Note: PrintStream.print(char) states

these bytes are written in exactly the manner of the write(int) method.

This doesn't make it clear that the flushing is different. It will flush if you have auto-flush OR write a new line.

System.out.print('a');
System.out.write('b');

prints just

a

I suspect this inconsistency is a long standing bug rather than a feature (in other words it won't be fixed). ;)

This means you have to either

System.out.flush();

or

System.out.write('\n');

to see the last line.

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
  • Wow. Can you please explain what is meant by line-buffered, and how does printing out a newline does that? – SexyBeast Jan 08 '13 at 11:21
  • for performance reasons, several IO methods keep a buffer. The output is performed after the buffer is full. Or you can force it to 'flush' to output the buffer elements. – Usman Saleem Jan 08 '13 at 11:23
  • See documentation as well: http://download.java.net/jdk7/archive/b123/docs/api/java/io/PrintStream.html#write(int) – Usman Saleem Jan 08 '13 at 11:26
  • 'to see the last line', does that mean that if I write ten lines without flushing, I will see the first 9? – SexyBeast Jan 08 '13 at 11:35
  • If you write ten lines (with a new line at the end) you will see them all. If you use `write(char)` to write nine full lines and some text after that without a new line, you will only see nine lines. If you use `print(char);` you will see all the text. – Peter Lawrey Jan 08 '13 at 12:15
1

Possibly, writing a new line \n flushes the stream.

in 2nd programs, you're not printing a newline and the contents are not flushed from stream. so you see no output.

See: When/why to call System.out.flush() in Java

Community
  • 1
  • 1
Azodious
  • 13,385
  • 1
  • 32
  • 68
  • Yeah that looks like the answer, can you explain why does printing out a newline flushes it? – SexyBeast Jan 08 '13 at 11:23
  • before flushing, data is stored in memory (limited size). `\n` or `flush()` is indicator to clear the contents of buffer memory. – Azodious Jan 08 '13 at 11:27