How to pass a stream to a java program?

In cmd.exe, I can do the following:

dir *.*|grep .... 

I want to do this with a java program

 dir *.i|java test 

What should I do in my Java test class?

+4
source share
2 answers

You processed the System.in stream and performed the capture / processing regardless of the source program ( dir in this case).

+1
source

Refer to the System.in class in Test , here is an 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(); } } } 
+1
source

Source: https://habr.com/ru/post/1438238/


All Articles