1

How do I redirect the input stream for a program so that it will run from the console as main > myProgram with no filename specified within the code? This is what I have so far.

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

I know it's going to be something to do with System.setIn but I have no clue as to how to write it so that it will detect a filename after Main > is typed.

8
  • 2
    Your question is not clear. Please rephrase. Commented Jan 25, 2014 at 4:40
  • 1
    Why can't you just pipe the data in in the first place? Commented Jan 25, 2014 at 4:43
  • There's no need to call setIn(...) or anything of that sort. The System.in will refer to the file that was piped into your program without your doing anything. Commented Jan 25, 2014 at 4:54
  • What kind of thing do i need to add so that it will point to the file? Commented Jan 25, 2014 at 4:58
  • java MyClass < myFile.txt will send myFile.txt to your MyClass program, assuming that you have a MyClass.class file there. Commented Jan 25, 2014 at 5:00

2 Answers 2

1

Use standard System.in and System.out streams.

Sign up to request clarification or add additional context in comments.

1 Comment

... and on the command line, use greater than and less than signs for file i/o redirection. Something like: java MyProgram < my.input > my.output
0

In order to redirect the streams, it is the methods of the System class: setIn(), setOut(), setErr() , which will help. now in order to redirect the stream you have to redirect it to the specific file type.

For instance if want to redirect the output stream, then you need to use the setOut(). the setOut() takes an object of print stream and print stream has a parameterized constructor which takes string, you will be providing the path in that manner.

here's the program which is linked to a test file. whenever you call the println() method on it, the stream will redirect it to the test file instead of the output console in this case.

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

}

And here is the program for the input stream:

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.