Java 打印BufferedReader返回的多行
我正在编写一个程序,基本上通过java发送linux命令,然后打印输出。如果输出只有一行,它就可以正常工作,但是对于多行输出,我不知道我做错了什么。例如,为了检查内存使用情况,我使用了“free”命令,但它只返回第1行和第3行。这是我的密码:Java 打印BufferedReader返回的多行,java,Java,我正在编写一个程序,基本上通过java发送linux命令,然后打印输出。如果输出只有一行,它就可以正常工作,但是对于多行输出,我不知道我做错了什么。例如,为了检查内存使用情况,我使用了“free”命令,但它只返回第1行和第3行。这是我的密码: if (clinetChoice.equals("3")) { String command = "free"; Process process = Runtime.getRuntime().exec(comman
if (clinetChoice.equals("3"))
{
String command = "free";
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
System.out.println("You Chose Option Three");
String line;
while ((line = reader.readLine()) != null)
{
output += line;
System.out.println(line);
line = reader.readLine();
}
}
当我运行此命令时,它只返回:
total used free share buffers cached
-/+ buffers/cache: 6546546 65464645
客户端代码:
while ((fromServer = input.readLine()) != null)
{
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye"))
break;
System.out.print("Enter your choice: ");
fromClient = stdIn.readLine().trim();
if(fromClient.equals("1"))
{
System.out.println("Client: " + fromClient);
output.println(fromClient);
}
if(fromClient.equals("2"))
{
System.out.println("Client: " + fromClient);
output.println(fromClient);
}
if(fromClient.equals("3"))
{
System.out.println("Client: " + fromClient);
output.println(fromClient);
}
if(fromClient.equals("4"))
{
System.out.println("Client: " + fromClient);
output.println(fromClient);
break;
}
}
您正在循环测试和循环体中调用
readLine
。因此,对于循环的每次迭代,readLine
被调用两次,其中一个结果被丢弃:它不会被打印或添加到输出中。这与您描述的结果相匹配
该循环应足够:
while ((line = reader.readLine()) != null)
{
output += line + System.getProperty("line.separator");
System.out.println(line);
}
如果您只想打印一次整个输出,并且由于您正在output
变量中收集输出,您可以将println
移出循环:
while ((line = reader.readLine()) != null)
{
output += line + System.getProperty("line.separator");
}
System.out.println(output);
您正在循环测试和循环体中调用readLine
。因此,对于循环的每次迭代,readLine
被调用两次,其中一个结果被丢弃:它不会被打印或添加到输出中。这与您描述的结果相匹配
该循环应足够:
while ((line = reader.readLine()) != null)
{
output += line + System.getProperty("line.separator");
System.out.println(line);
}
如果您只想打印一次整个输出,并且由于您正在output
变量中收集输出,您可以将println
移出循环:
while ((line = reader.readLine()) != null)
{
output += line + System.getProperty("line.separator");
}
System.out.println(output);
只要用这个。。。您正在调用readLine()
两次
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
如果要将数据分配给输出变量..,请在while循环中执行此操作
output=输出+行代码>只需使用此。。。您正在调用readLine()
两次
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
如果要将数据分配给输出变量..,请在while循环中执行此操作
output=输出+行代码>我应该指出的是,除了注释之外。使用readline()
两次,您应该严格同时使用stdout/stderr。否则,由于您没有使用流程输出,因此您将面临阻塞流程输出的风险。有关更多信息,请参阅。我应该指出,除了注释之外,您还可以阅读。使用readline()
两次,您应该严格同时使用stdout/stderr。否则,由于您没有使用流程输出,因此您将面临阻塞流程输出的风险。有关更多信息,请参阅。这个答案是正确的,只需删除line=reader.readLine()代码>在循环的底部。这很有效,谢谢。唯一的问题是,它返回同一行中的所有内容。我添加了输出+=行+“\n”;但这会一次打印一行,而不是一次打印所有内容。@尼克:readLine
会占用换行符,因此您需要将其添加回。如果要一次打印所有输出,请删除循环中的println
,并在循环后执行println(输出)
。我将把这个添加到回答中。我应该提到,我在客户机/服务器程序之间使用套接字通信。所以基本上从客户端程序开始,我要求服务器程序运行“free”命令,并将其打印回客户端程序。使用line.separator时,我仍然得到与使用“\n”时相同的结果,它打印一行,然后打印第二行。。。而不是一次打印所有内容。我将把我的客户端代码添加到最上面的帖子中。这个答案是正确的,只需去掉line=reader.readLine()代码>在循环的底部。这很有效,谢谢。唯一的问题是,它返回同一行中的所有内容。我添加了输出+=行+“\n”;但这会一次打印一行,而不是一次打印所有内容。@尼克:readLine
会占用换行符,因此您需要将其添加回。如果要一次打印所有输出,请删除循环中的println
,并在循环后执行println(输出)
。我将把这个添加到回答中。我应该提到,我在客户机/服务器程序之间使用套接字通信。所以基本上从客户端程序开始,我要求服务器程序运行“free”命令,并将其打印回客户端程序。使用line.separator时,我仍然得到与使用“\n”时相同的结果,它打印一行,然后打印第二行。。。而不是一次打印所有内容。我将把我的客户代码添加到最上面的帖子中。