Question

Can someone show me code of something simple that pipes the output from one java file to the input of another in Java?

Say you have a file that is called hello.java that just simply outputs "hello". How would I pipe this output from the command line into another Java program called addWorld.java that simply adds "world" to the end of the input from hello.java and then outputs "hello world" on the console screen?

I'm sure it is very simple, but I've looked around and I still don't understand how to do it. I've tried to make an example that is as simple as possible so there isn't a lot of code written so I can just understand what to do in a general case. Thanks.

No correct solution

OTHER TIPS

You'll want to start the second java program using run time probably and add the output for the first command as a parameter for running the second, as from what I've tried Windows does not like piping into other programs.

Quick how-to: https://stackoverflow.com/a/8496537/3342157
Information on Runtime: http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

On Windows, in the command line, this is done with the redirection operator (>)

It is done as so:

java hello > java addWorld

This would take the stdout of hello.java (anything printed using System.out) into the stdin of addWorld.java as a command line argument (the args part of public static void main(String[] args))

EDIT: The pipe character (|) filters the stdout of the program through a program. If sort sorts a program alphabetically based on its stdin, typing this:

simon-answers.txt | sort > simon-answers-sorted.txt

into a windows command prompt would take the lines in simon-answers.txt ("Simon says" commands) and pipe (|) them through sort and then redirect (>) them to the text file simon-answers-sorted.txt

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top