I've written a utility in Java that filters JSON input, from a file or from stdin, to XML. Though I've made it a stand-alone, runnable JAR, I'm lost as to how to redirect input to it via stdin. For example:
cat sample.json | java -jar ./target/json-to-xml-1.0.0.jar
This command line doesn't work; I wonder if this can even be done.
private static String readContentFromStdIn() throws IOException
{
char ch;
StringBuilder sb = new StringBuilder();
PipedInputStream input = new PipedInputStream();
while( ( ch = ( char ) input.read() ) != -1 )
sb.append( ch );
input.close();
return sb.toString();
}
asked Dec 19, 2014 at 17:20
Russ Bateman
18.7k14 gold badges53 silver badges68 bronze badges
1 Answer 1
Your code is creating a PipedInputStream which is not connected to anything and will never get any data. For reading from stdin, you don't need to create a new stream; you should just read from System.in.
answered Dec 19, 2014 at 17:59
yole
98.1k21 gold badges279 silver badges205 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Russ Bateman
Thank you very much! I adopted Scanner sc=new Scanner(System.in) to read from stdin and it now works.
lang-java
System.in? (please paste the relevant code)