本文介绍了函数停止我的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PrintWriter 的打印功能.当我使用这种方法时,我的程序继续运行,但我的所有其他功能都不起作用.

I am trying to use the print function of PrintWriter. When I use this method, my program continues to run but all my other functions doesn't work.

    public void printVertices(PrintWriter os) {
        for(int i = 0; i < vert.size(); i++) {
            os.print(vert.get(i) + " ");
        }
        os.close();
    }

推荐答案

同样,问题是你关闭 PrintWriter,它也关闭了底层的OutputStreamWriter.

Again, the problem is that you close the PrintWriter, which also closes the underlying OutputStream or Writer.

您可能添加了 os.close(); 语句,否则 PrintWriter 将缓冲输出,您将看不到任何打印到控制台的内容.

You probably added the os.close(); statement, because otherwise the PrintWriter will buffer the output and you won't see anything printed to the console.

关闭 PrintWriter 将首先 flush() 它,然后关闭它.你可能不想要第二部分.

Closing a PrintWriter will first flush() it, then close it. You probably don't want the second part.

因此,解决方案是简单地flush PrintWriter:

The solution therefore is to simply flush the PrintWriter:

public void printVertices(PrintWriter os) {
    for(int i = 0; i < vert.size(); i++) {
        os.print(vert.get(i) + " ");
    }
    os.flush();
}

这篇关于函数停止我的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 09:24