- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码:
public static void main(String[] args) {
String in = "";
InputStreamReader reader = new InputStreamReader(System.in);
int ch;
StringBuilder sb = new StringBuilder();
System.out.println("paste text below:");
try {
while ((ch = reader.read()) != -1 && ch != 0) {
sb.append((char)ch);
}
}catch (IOException e) {
System.err.println(e.toString());
}
}
in = sb.toString();
System.out.println(in);
}
我已经调试过,它会遍历所有内容并在换行符上打印每个字符,但在读取最后一个字符后它总是会停止。我简单地输入了 asdfasdf
以及复制并粘贴《独立声明》。它一直运行到结束,然后停止。
这甚至不像是一个无限循环,因为我在代码的每个部分都放置了一个 System.out.print("!@#$%") ,它就停止了......我的代码中没有无限循环代码。我非常确定InputStreamReader
-- reader
-- 陷入了它自己的无限循环中,因为它永远不会返回 -1。事实上,当它到达末尾时,它永远不会返回任何内容(尽管它在循环中返回正确的整数/字符)。
有谁知道发生了什么事或者有人遇到过类似的问题吗?有没有办法解决这个问题(不要说 BufferedReader 因为我需要读取带有多个换行符的文本,并且 BufferedReader 搜索第一个 \n
,所以我无法使用它)?
编辑:
我尝试读取字符缓冲区,然后使用字符串生成器......什么也没有。
您必须提供一个将打破循环的字符(否则请参阅 @default locales 的 answer )。
这是我的代码演变为:
public static void main(String[] args) {
InputStreamReader reader = new InputStreamReader(System.in);
StringBuilder sb = new StringBuilder();
int ch;
System.out.println("Paste text below (enter or append to text \"ALT + 1\" to exit):");
try {
while ((ch = reader.read()) != (char)63 /*(char)63 could just be ☺*/) {
sb.append(ch);
}
reader.close();
}catch (IOException e) {
System.err.println(e.toString());
}
String in = sb.toString();
System.out.println(in);
}
Alt + 1
returns (至少在 Windows 上)返回笑脸图标,但是您可以执行任何您想要的组合键,您所要做的就是找出 java 中的 char 并执行 while ((ch = reader.read()) != (char)charNumber
.
最佳答案
基本上,reader.read()
内部调用InputStream.read 。以下是文档中的引用:
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
换句话说,此方法将阻止程序执行并永远等待您的输入。
现在您可以看到当前状态下的程序永远不会停止执行:
//wait for user input forever
while ((ch = reader.read()) != -1 && ch != 0) {
//no matter what the input is just print it
System.out.println((char)ch);
} //continue waiting...可能的解决方案:
- 自定义退出策略,例如当用户输入
Ω
时中断循环,或者当用户连续输入“KILL ME”三次,或者在第 200 个字符之后。- 在命令窗口中输入特殊的文件结束组合来关闭
System.in
。这种方法通常涉及一些特定于操作系统/终端的黑客攻击。看看这个问题:How to send EOF via Windows terminal .- 实现非阻塞读取机制。看看这个问题:Is it possible to read from a InputStream with a timeout?
之前已经在 Stackoverflow 上提出过类似的问题:
关于Java InputStreamReader 不变的停顿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25040921/
首先给大家介绍InputStreamReader 和FileReader的区别,具体内容如下所示: InputStreamReader 和 BufferedReader 。其中最重要的类是 Inp
我在主要 Activity 的 onCreate() 方法中编写了以下内容。 file = new File(this.getFilesDir(), SIMPLE_WORKOUTS); wri
这是我的代码: public static void main(String[] args) { String in = ""; InputStream
我正在尝试从 Socket 读取 UTF-16 字符串。关键是在读取字符串的前几个字符之前我不知道它的长度(以字节为单位)。 所以我像这样打开了InputStreamReader: InputStre
我在 Android 2.2 手机上遇到了一些奇怪的问题。 我打开套接字连接: socket = new Socket(serverAddr, SERVERPORT); messageWriter =
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我在类里面设计的一个小游戏有问题。问题是我有两个客户端连接到服务器。 (client1 和 client2)他们每个人都在运行一个游戏,最后关闭窗口。由于游戏窗口是 JDialog,因此当它关闭时,它
我正在努力学习 Java 并浏览 Android 网站上的示例。我正在获取 XML 文件的远程内容。我能够获取文件的内容,但随后我需要将 InputStream 转换为 String。 public
我想 ping 目标 IP 地址并接收响应。为此,我在 Java 中使用带有 runtime.exec 方法和进程类的 Windows 命令行。我正在使用 inputStreamReader 获取响应
我正在从一个文件中读取数据,不幸的是,该文件具有两种类型的字符编码。 有标题和正文。 header 始终采用 ASCII 格式,并定义了主体编码所用的字符集。 header 的长度不固定,必须通过解析
有没有人碰巧知道下面两种读取输入文件的方法在性能方面是否存在差异?谢谢。 1)用Scanner和File读取文件 Scanner input = new Scanner(new File("foo.t
我有一个正在运行的服务器,它接受通过客户端套接字建立的连接,我需要从该客户端套接字读取输入,现在假设客户端打开了到我的服务器的连接,而不通过服务器的套接字输出流发送任何内容,在这种情况下,当我的服务器
我正在查看java.io,有些方面让我感到困惑: FileReader 和 InputStreamReader 之间有性能差异吗? Reader fileReader = new FileReader
我有一个 InputStreamReader(与 BufferedReader 结合使用)。 我想循环读取Stream的内容 while((line = MyStream.readLine()) !=
Java 中的常见模式是使用 InputStreamReader 从文件中读取字符并将其附加到 StringBuilder;最明显的方法是: int c = reader.read(); sb.app
所以我有一个用 Java 编写的非常简单的服务器: public class SimpleServer { public static void main(String[] args) thr
我必须在读取文件之前检查文件的编码。为了检查编码,我使用了这个方法: try { CharsetDecoder decoder= Charset.forName
我正在尝试通过套接字将字符串从我的 java 服务器发送到 android 客户端。 InputStreamReader 的读取方法返回的不是显示正确的字符串“OK”“�OK”。 服务器的代码如下所示
我在一个项目中基本上有这些代码行,它将输入流复制到输入流读取器中,以便它可以独立流式传输: final InputStream stream = new InputStream(this.in);
我正在尝试解析一个 UTF 16 编码的 JSON 文件,但是我遇到了一个奇怪的问题。 每当我使用 FileInputStream 时,解析文件似乎都从中点开始。例如,如果文件的长度为 40 个字符,
我是一名优秀的程序员,十分优秀!