user1626579
user1626579

Reputation: 41

How to pipe stream to java program?

Under cmd.exe, I can do like this:

dir *.*|grep ....

I want to do this to java program

dir *.i|java test 

What I should do in my java test class?

Upvotes: 4

Views: 159

Answers (2)

Koerr
Koerr

Reputation: 15723

Handle the System.in in Test class,here is the example:

public class Test {
    public static void main(String[] args) {
        InputStream in = System.in;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int next = in.read();
            while (next > -1) {
                bos.write(next);
                next = in.read();
            }
            bos.flush();
            byte[] bytes = bos.toByteArray();
            System.out.println("output:" + new String(bytes));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 1

Tasos P.
Tasos P.

Reputation: 4114

You would process the System.in stream and capture/process whatever the source program (dir in this case) provides.

Upvotes: 1

Related Questions