Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - the difference between BufferedWriter.write() and System.out.println()?

Tags:

java

I have been trying to solve an algorithm problem called Chebyshev's Theorem(https://www.acmicpc.net/problem/4948)

And I got an interesting situation. I haven't figured out the difference between yet. And I hope I can find the answer here.

And this is my code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class Solution {
    public static void main(String[] args)  throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        while(true){
            System.out.println("start!!");
            int input = Integer.parseInt(br.readLine());        
            if(input == 0){
                break;
            }
            int ddable = 2*input;
            int answer = 0;
            ArrayList<Integer> base = new ArrayList<>();

            if(input == 1){
                answer = 1;
            }else { 
                System.out.println("It is not 1");
                for(int i = 2 ; i <= ddable ; i++ ){
                    base.add(i);
                }

                for ( int i = 2 ; i <= ddable ; i++ ){
                    for ( int j = 2 ; i*j <=ddable ; j++){
                        if(base.contains(new Integer(i*j))){
                            base.remove(new Integer(i*j));
                            System.out.println(i*j+"removed");
                        }
                    }
                }
                int count = 0;
                for ( int i = input ; i <= ddable ; i++){
                    if(base.contains(new Integer(i))){
                        count++;
                    }
                }
                answer = count;
            }
            System.out.println("syso: "+answer);
            bw.write("bw: "+answer);
        }
        bw.flush();
        br.close();
        bw.close();
    }
}

And this is my result:

enter image description here

As you can see, this just shows the result of 'System.out.prinln()'.

What's the reason?

like image 354
c-an Avatar asked Jan 02 '23 05:01

c-an


2 Answers

See the doc:

Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

You can call flush or close to make sure the content be printed.

like image 54
xingbin Avatar answered Feb 02 '23 11:02

xingbin


This was a simple problem.

To print something out with 'BufferedWriter', you need to call 'flush()' after in the right time you want to print it out. So, in the code.

You need to fix this part.

    System.out.println("syso: "+answer);
    bw.write("bw: "+answer);
}
bw.flush();

to

    System.out.println("syso: "+answer);
    bw.write("bw: "+answer);
    bw.flush();
}

Then, It prints the result that you want and even if the loop runs, it prints fine without any problem.

And check this for 'flush()' : What is the purpose of flush() in Java streams?

It says

Flushes the output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

So, to be written out, you need to call 'flush()' after 'write()'. And 'bw' is still reusable if it hasn't closed('close()') yet.

like image 22
c-an Avatar answered Feb 02 '23 13:02

c-an