Skip to content

Instantly share code, notes, and snippets.

@sarnobat
Last active August 28, 2018 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarnobat/2c00c27e699166a95fdd00ffcb571b4e to your computer and use it in GitHub Desktop.
Save sarnobat/2c00c27e699166a95fdd00ffcb571b4e to your computer and use it in GitHub Desktop.
Java command line program that loops over stdin
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