Skip to content

Instantly share code, notes, and snippets.

@youssefeldakar
Created September 9, 2019 09:05
Show Gist options
  • Save youssefeldakar/ec616127a6abe36360b2675f8d571169 to your computer and use it in GitHub Desktop.
Save youssefeldakar/ec616127a6abe36360b2675f8d571169 to your computer and use it in GitHub Desktop.
Java: Execute process and prefix output lines with 'o' or 'e'
import java.lang.*;
import java.io.*;
public class Exec {
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("ls /vmlinuz /mvlinuz");
try {
process.waitFor();
} catch(InterruptedException e) {
e.printStackTrace();
}
BufferedReader ir = new BufferedReader(new InputStreamReader(process.getInputStream(), "utf8"));
BufferedReader er = new BufferedReader(new InputStreamReader(process.getErrorStream(), "utf8"));
String s = null;
while((s = ir.readLine()) != null) {
s = "o: " + s;
System.out.println(s);
}
while((s = er.readLine()) != null) {
s = "e: " + s;
System.out.println(s);
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment