简体   繁体   中英

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

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:

在此输入图像描述

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

What's the reason?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM