简体   繁体   中英

Why does my code not print anything?

I was looking for some efficient method to print my output, and I found this article.

However I used the following code to test it, but it doesn't display any output.

import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.IOException;
class NewClass {
    public static void main(String args[] ) throws Exception {
        Printy p=new Printy();
        p.printLine("JAVA");
    }
}

class Printy
{
    private final BufferedWriter bw;
    public Printy()
    {
        bw=new BufferedWriter(new OutputStreamWriter(System.out));
    }
    public void print(String str)throws IOException
    {
        bw.append(str);
    }
    public void printLine(String str)throws IOException
    {
        print(str);
        bw.append("\n");
    }
    public void close()throws IOException
    {
        bw.close();
    }
} 

What is wrong here and how to implement it properly?

You need to flush the buffer, or else the text will sit in the buffer and not be printed.

Add a call to flush after you append the text.

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