- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
好吧,这可能不是最好的问题,但我一直坚持下去,无法在网上找到答案。
此代码不会第二次从标准输入读取:
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))
{
input = br.readLine();
}
catch (final Exception e)
{
System.err.println("Read from STDIN failed: " + e.getMessage());
}
// do some processing
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))
{
input = br.readLine();
}
catch (final Exception e)
{
System.err.println("Read from STDIN failed: " + e.getMessage());
}
我知道 java 的 try-with-resources 会递归地关闭链中的所有流,因此在第一次读取 System.in
后关闭。有什么好的解决方法吗?还是我真的应该自己处理流关闭?
更新:我试图自己处理关闭的流(即 java6 风格)。这是一个 code如果有人感兴趣。但我注意到这种链关闭行为不是来自 try-with-resources,而是来自关闭方法的实现。所以我没有从那次尝试中赢得任何东西。
我选择 fge 的解决方案,因为它是最冗长的解决方案。它直接对我有用。
总而言之,我觉得很奇怪,java 没有开箱即用的解决方案,因为存在不应该关闭的系统流。
最佳答案
一种解决方法是创建一个自定义 InputStream
类,该类将委托(delegate)给另一个类,但它在自身关闭时不会 .close()
它。如:
public class ForwardingInputStream
extends InputStream
{
private final InputStream in;
private final boolean closeWrapped;
public ForwardingInputStream(final InputStream in, final boolean closeWrapped)
{
this.in = in;
this.closeWrapped = closeWrapped;
}
public ForwardingInputStream(final InputStream in)
{
this(in, false);
}
@Override
public int read()
throws IOException
{
return in.read();
}
@Override
public int read(final byte[] b)
throws IOException
{
return in.read(b);
}
@Override
public int read(final byte[] b, final int off, final int len)
throws IOException
{
return in.read(b, off, len);
}
@Override
public long skip(final long n)
throws IOException
{
return in.skip(n);
}
@Override
public int available()
throws IOException
{
return in.available();
}
@Override
public void close()
throws IOException
{
if (closeWrapped)
in.close();
}
@Override
public synchronized void mark(final int readlimit)
{
in.mark(readlimit);
}
@Override
public synchronized void reset()
throws IOException
{
in.reset();
}
@Override
public boolean markSupported()
{
return in.markSupported();
}
}
请注意,在您的情况下,一个可能更简单的解决方案是扩展 InputStreamReader
,因为该类不是 final
并且只是覆盖 .close()
.
关于java - 试用资源和 System.in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23041258/
冷静下来,不吹不黑。 01 最近半年,互联网一款现象级的应用诞生:「ChatGPT」; 其火爆的程度,不输前面的羊了个羊; 最初了解到ChatGP
在了解在 .Net 中开发是什么样子之前,我有几个新手问题: 非管理员用户能否安装 .Net 框架,无论是原始包还是运行程序所需的任何后续更新?我们的一些客户锁定了 XP/Vista 主机,当我们上传
https://plus.google.com/u/1/110412141990454266397/posts/Nvr6Se6eAPh 有谁知道如何使用最新版本的 GWT(来自 svn)和 Chrom
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是偏离
我需要在加载 NSDocument 之前创建多个窗口,或者创建一个阻止 NSDocument 窗口和顶部菜单的窗口。 我尝试了几种解决方案 - 但它们都不起作用。 模态窗口,一个接一个。 Async
尝试配置 spring 3 MVC,这是我目前所做的: 我将所有 spring 3.0 库添加到我的 netbeans 项目中。 我的 web.xml 是: WebApp
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我无法让 Resharper 的清理代码停止更改如下属性: public long Value { get { return _thingy; } se
我并不是真的在寻找基本的 SQL 指南,而是寻找特定于 PostgreSQL 的东西。 而且我确实运行自己的服务器,因此获取最新版本(我相信是 8.2?)没问题。 最佳答案 重要的事情: 如果可以(既
这是 Adobe Encore 程序的屏幕截图。 在这里,您可以看到一组深色组件。 可以购买,下载等吗?它们是从 Adobe Cor. 公开的吗? 谢谢 最佳答案 实现这种外观和感觉的最简单方
我有一个代码(可以正常工作)用于使用 C++ Builder 来引导 word。它对于访问文档中的不同书签很有用。 Variant vNom, vWDocuments, vWDocument, vMS
我是一名优秀的程序员,十分优秀!