Last active
August 28, 2018 19:18
-
-
Save sarnobat/2c00c27e699166a95fdd00ffcb571b4e to your computer and use it in GitHub Desktop.
Java command line program that loops over stdin
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.io.*; | |
public class StdinLoop { | |
public static void main(String[] args) { | |
BufferedReader br = null; | |
try { | |
br = new BufferedReader(new InputStreamReader(System.in)); | |
String line; | |
while ((line = br.readLine()) != null) { | |
// log message | |
System.err.println("[DEBUG] current line is: " + line); | |
// program output | |
System.out.println(line); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
if (br != null) { | |
try { | |
br.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment