Created
September 9, 2019 09:05
-
-
Save youssefeldakar/ec616127a6abe36360b2675f8d571169 to your computer and use it in GitHub Desktop.
Java: Execute process and prefix output lines with 'o' or 'e'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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