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

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

问题描述

这是一段简单的代码:

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);
  }
 }
}

我将其执行为:java Read <输入.txt

I execute this as : java Read < input.txt

一旦 input.txt 完全通过管道传输到程序中,x 就会不断获得无限的空值.为什么呢?在将文件输入代码完成后,有没有办法使标准输入(命令行)处于活动状态?我试过关闭流并重新打开,它不起作用.重置等等.

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 你已经告诉操作系统,对于这个进程,管道文件标准.然后你不能从应用程序内部切换回命令行.

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天全站免登陆