Java中的命令行管道输入 [英] Command Line Pipe Input in Java

查看:944
本文介绍了Java中的命令行管道输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一段简单的代码:

Here is a simple piece of code:

import java.io.*;
public class Read {
 public static void main(String[] args) {
     BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
  while(true)
  {
   String x = null;
   try{
    x = f.readLine();
   }
   catch (IOException e) {e.printStackTrace();}
   System.out.println(x);
  }
 }
}

阅读& input.txt

I execute this as : java Read < input.txt

一旦input.txt完全输入到程序中,x会保持无限的null。为什么会这样呢?有没有办法,我可以使标准输入(命令行)激活后,文件被送入代码完成?
我试过关闭流并重新打开,它不工作。重置等。

Once the input.txt is completely piped into the program, x keeps getting infinite nulls. Why is that so? Is there a way by which I can make the Standard In(Command Line) active after the file being fed into the code is done? I've tried closing the stream and reopening, it doesn't work. Reset etc also.

推荐答案

通过执行java Read< input.txt您告诉操作系统,对于此过程,管道文件标准。您不能切换回到应用程序内部的命令行。

By executing "java Read < input.txt" you've told the operating system that for this process, the piped file is standard in. You can't then switch back to the command line from inside the application.

如果你想这样做,然后将input.txt作为文件名参数传递给应用程序,从应用程序内部自己打开/读取/关闭文件,然后从标准输入读取从命令行获取东西。

If you want to do that, then pass input.txt as a file name parameter to the application, open/read/close the file yourself from inside the application, then read from standard input to get stuff from the command line.

这篇关于Java中的命令行管道输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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