Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-重定向输入流_Java_Inputstream - Fatal编程技术网

Java-重定向输入流

Java-重定向输入流,java,inputstream,Java,Inputstream,如何重定向程序的输入流,使其以main>myProgram的形式从控制台运行,而代码中没有指定文件名?这就是我目前所拥有的 public static void main (String[ ] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); while(in.next != null){ in.read(

如何重定向程序的输入流,使其以main>myProgram的形式从控制台运行,而代码中没有指定文件名?这就是我目前所拥有的

public static void main (String[ ] args) throws IOException { 
    BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); 
    while(in.next != null){
        in.read();
    }
}

我知道这与System.setIn有关,但我不知道如何编写它,以便在键入Main>后它能检测到文件名。

使用标准
System.in
System.out
流。

为了重定向流,是系统类的方法:setIn()、setOut()、setErr(),这会有帮助的。 现在,为了重定向流,您必须将其重定向到特定的文件类型

例如,如果要重定向输出流,则需要使用setOut()。setOut()接受print stream的一个对象,print stream有一个参数化的构造函数,它接受string,您将以这种方式提供路径

这是链接到测试文件的程序。 无论何时对其调用println()方法,流都会将其重定向到测试文件,而不是本例中的输出控制台

import java.io.FileNotFoundException;
import java.io.PrintStream;

public class Redirect1 {

    public static void main(String[] args)throws FileNotFoundException {
        System.setOut(new PrintStream("C:\\Users\\nEW u\\Desktop\\Test.txt"));

        System.out.println("Hello");
    }

}
下面是输入流的程序:

import java.io.*;
import java.lang.System;

public class Redirect {

    public static void main(String[] args)throws IOException {
        System.setIn(new FileInputStream("C:\\Users\\nEW u\\Desktop\\dev.txt"));
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s="";
        while((s=br.readLine())!=null)
        System.out.println(s);
    }

}

你的问题不清楚。请重新措辞。为什么您不能首先通过管道将数据导入?不需要调用
setIn(…)
或任何类似的东西。System.in将引用在您不做任何操作的情况下通过管道传输到您的程序中的文件。我需要添加什么样的内容以使其指向该文件?
java MyClass
将向您的MyClass程序发送myFile.txt,假设您有MyClass.class文件。。。。在命令行上,使用大于和小于符号进行文件i/o重定向。类似于:
javamyprogrammy.output