我有这个代码
Process p =Runtime.getRuntime().exec("busybox");
InputStream a = p.getInputStream();
InputStreamReader read = new InputStreamReader(a);
BufferedReader in = new BufferedReader(read);
从终端运行它,oupout 的第一行返回 Busybox 的版本。如果我想像我一样以前 5 个字符为例?
最佳答案
虽然其他答案也应该很好地工作,但以下内容将在读取五个字符后退出并关闭流:
Process p = Runtime.getRuntime().exec("busybox");
InputStream a = p.getInputStream();
InputStreamReader read = new InputStreamReader(a);
StringBuilder firstFiveChars = new StringBuilder();
int ch = read.read();
while (ch != -1 && firstFiveChars.length() < 5) {
firstFiveChars.append((char)ch);
ch = read.read();
}
read.close();
a.close();
System.out.println(firstFiveChars);
关于java - 如何从缓冲读取器读取前五个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19063428/