Java 带有System.in的FileInputStream
我想这样重定向stdinJava 带有System.in的FileInputStream,java,Java,我想这样重定向stdin java foo < test.txt 这应该非常简单,但我一直在绞尽脑汁,因为我想不出一个不使用args[x]就获取test.txt的方法 编辑:问题是我正在这样做: FileInputStream readByte = new FileInputStream(file); char c = (char)readByte.read(); 它所提供的信息与 char c = System.in.read(); 不确定这是否正是您想要的,但请检查以下问题:
java foo < test.txt
这应该非常简单,但我一直在绞尽脑汁,因为我想不出一个不使用args[x]就获取test.txt的方法
编辑:问题是我正在这样做:
FileInputStream readByte = new FileInputStream(file);
char c = (char)readByte.read();
它所提供的信息与
char c = System.in.read();
不确定这是否正是您想要的,但请检查以下问题: 他们使用以下方法:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Printing the file passed in:");
while(sc.hasNextLine()) System.out.println(sc.nextLine());
}
嗯,如果它来自
System.in
,它就不是FileInputStream
,因为System.in
不是文件
。System.in已经是一个InputStream。您可以直接从中读取字节。我知道可能与System.in.read()重复,但由于某些原因System.in.read()提供的字节数与FileInputStream的字节数不同。readByte()您阅读了文档吗?一个读取字节,另一个读取字符。不,如果我不够具体,很抱歉,但问题是我正在这样做:FileInputStream readByte=new FileInputStream(file);char c=readByte.read();它没有给出与char c=System.in.read()相同的东西;复制并重新发布@Akash我知道,我已经标记了这个问题。我也充分引用了他们的答案。我只是为了OP才发的
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Printing the file passed in:");
while(sc.hasNextLine()) System.out.println(sc.nextLine());
}