Function stops my program

半世苍凉 提交于 2019-12-13 09:18:40

问题


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();
    } 

回答1:


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

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.

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

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();
}


来源:https://stackoverflow.com/questions/49608991/function-stops-my-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!