printing Integer using BufferedOutputStream in java

Need to print a million values in console, System.out.println() is comparatively slow when compared to BufferedOutputStream(from what I read).

So I tried to print this way:

OutputStream out = new BufferedOutputStream (System.out);

Since sum is of the type int, and the out.write() is printing in bytes. I tried to convert bytes to decimal, which gave me a compilation error saying:

byte cannot be dereferenced

So, my problem is: to print integer values in decimal format using BufferedOutputStream.

Since, I did not find any solution here in printing int values using BufferedOutputStream, I am desperately looking for an answer.


The problem you have is that displaying text on the console is much slower than generating it. You can use a buffer and if redirected to a file, instead of the screen this will be faster, however if you are actually displaying on the screen it won't be much faster. What buffering can do is reduce the amount of scrolling since it writes to the screen in lumps of text instead of one line at a time.

Note: BufferedOutputStream writes binary not text. It will be faster but is not human readable. If you want human readable I suggest using PrintWriter which is designed to write text.

PrintWriter out = new PrintWriter(new BufferedWriter(
                                  new OutputStreamWriter(System.out, "UTF-8")));

// call many times

// to flush the buffer so all the data can be read.

You could use a StringBuilder, however this is;

  • slower as you are not displaying text until the whole string has been created. As noted, the displaying of the text is your main problem.
  • uses as much memory as the output, and doesn't scale well.

Hope this will help...

byte[] ascii = Integer.toString(sum).getBytes();

OutputStream console = new BufferedOutputStream(System.out);

try {



} catch (IOException e) {



But you'll have to check if this code is faster than


use BigInteger(bytes), it will get you the integer in a BigInteger format.

Need Your Help

Testing Spring managed servlet

java spring servlets spring-test

I need to test a servlet, which is working fine now.

Replacing matrix rows for a vector without using a loop

arrays matlab indexing

Is it possible to replace some rows of a matrix for specific vectors (in my case, random vectors) without using a loop in MATLAB?