System.out 关闭了吗?我可以重新打开它吗? [英] System.out closed? Can I reopen it?

查看:29
本文介绍了System.out 关闭了吗?我可以重新打开它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在帮助一个朋友编写一些 Java 代码,他对 Java 不太了解.所以我给他写了一些辅助函数来轻松完成他眼中有些古怪的事情.其中之一是将字符串写入 OutputStream 的函数.看看:

I was helping a friend to write some Java code, who doesn't know a lot about Java. So I wrote him some helper functions to easily get things done that are a little quirky in his eyes. One of them is a function, that writes a String to an OutputStream. Have a look:

public void write(String txt, OutputStream out) {
    PrintWriter printer = new PrintWriter(out);
    printer.print(txt);
    printer.close();
}

现在,您可以轻松地以不同的方式使用它来随心所欲地写作.例如你可以这样做:

Now, you can use that easily in different ways to write wherever you want. For example you could do that:

(new StreamHelper()).write("Hello Test", System.out);

这样做之后我发现 System.out.println() 不再向 shell 写入任何内容.所以我想也许 printer.close() 也自动关闭了 System.out ,我想知道如何重新激活它,以便在此功能再次完成后我可以使用它.

Doing that I found out that afterwards System.out.println() doesn't write anything to the shell anymore. So I think that maybe printer.close() automatically also closed System.out and I wonder how to reactivate it that I can use it after this function is finished again.

我的假设正确吗?(不在这里问我怎么知道的?)

Is my assumption correct? (How could I have found out without asking here?)

如何在调用 write() 函数后继续使用 System.out?

How can I continue to use System.out after a call to the write() function?

有没有更好的方法来编写这样的辅助函数?

Are there better ways to write such a helper function?

推荐答案

OutputStream 关闭的一般契约:

public void close()throws IOException 关闭此输出流并释放与此流关联的任何系统资源.一般合同close 是它关闭输出流.关闭的流不能执行输出操作并且无法重新打开.

public void close() throws IOException Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

PrintStream's

public void close() 关闭流.这是通过冲洗流,然后关闭底层输出流.

public void close() Close the stream. This is done by flushing the stream and then closing the underlying output stream.

我能给你的唯一建议是你不应该写非对称代码<​​/a>,也就是说,不要将您的代码创建的资源的关闭委托给其他地方.

The only advice I can give you is that you should not write asymmetrical code, that is, don't delegate the closing of resources your code has created to somewhere else.

即使在您的情况下关闭包装流似乎是明智的,但事实是您不应该这样做,因为您正在关闭在其他地方打开的流.

Even if in your case it might seemed sensible to close the wrapper stream, the fact is that you should not because you are closing a stream opened somewhere else.

简而言之:

public void write(String txt, OutputStream out) {
    PrintWriter printer = new PrintWriter(out);
    printer.print(txt);
    printer.flush();
    //it is very unpolite to close someone else's streams!
    //printer.close();
}

哦,顺便说一句,你可能想把函数名改成print,而不是write.

Oh, and by the way, you may want to change the function name to print, rather than write.

这篇关于System.out 关闭了吗?我可以重新打开它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆