Uso de System.in.read() ReadCharacter.java // la clase IOException pertenece a java.io import java.io.*; public class ReadCharacter { public static void main(String[] args) throws IOException { System.out.print("Enter character: "); // System.in.read() throws an IOException, // which the method calling it -- main --- throws back // we don't worry about this for now!! char c = (char) System.in.read(); System.out.println("\nYou entered: " + c); } } EOF ReadCharacters1.java // la clase IOException pertenece a java.io import java.io.*; // this program must be executed under a DOS windows (not netbeans) // as EOF (Ctrl-Z) doesn't work in Netbeans output window class ReadCharacters1 { public static void main(String args[]) throws IOException { String cadena = ""; System.out.println("Enter characters (Ctrl-Z on new line to finish)!!"); // the Ctrl-Z command will only work in a DOS window, // not in Scite environment (or netBeans) int c = System.in.read(); while (c != -1) { cadena = cadena + (char)c; c = System.in.read(); } // the method trim remove the blanks at the end of a string System.out.println("You typed in: [" + cadena.trim() + "]" ); } } System.in.read() e IOException ReadCharacters0.java Gestión de Excepciones ReadCharacters2.java // la clase IOException pertenece a java.io import java.io.*; // en vez de lanzar la excepción lanzada por System.in.read() // fuera del main (como en ReadCharacter2) // atrapamos el error y imprimimos un mensaje class ReadCharacters2 { public static void main(String args[]) { try { String cadena = ""; System.out.println("Enter characters (Ctrl-Z on new line to finish)!!"); int c = System.in.read(); while (c != -1) { cadena = cadena + (char)c; c = System.in.read(); } System.out.println("You typed in: [" + cadena.trim() + "]" ); } catch (IOException ioe) { System.out.println("Problem reading character from the standard input."); } } Leer línea por línea del flujo estándar de entrada SystemIn.java import java.io.*; class SystemIn { public static void main(String[] args) throws IOException { String line; InputStreamReader s = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(s); System.out.print("Enter your input: "); line = stdin.readLine(); System.out.println( "You typed: " + line ); s.close(); stdin.close(); } } Sumar diez números entrados por el flujo estándar SumarDiez.java import java.io.*; class SumarDiez { public static void main(String[] args) throws IOException { String line; InputStreamReader s = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(s); int count = 1; int sum = 0; System.out.println("Please introduce numbers, one per line."); while (count <= 10) { // System.out.println("Enter number " + count + ":"); line = stdin.readLine(); sum = sum + Integer.parseInt(line.trim()); count++; } System.out.println("You typed in 10 numbers. The sum of the numbers is " + sum); s.close(); stdin.close(); } } Sumar N números entrados por el flujo estándar Sumar.java import java.io.*; class Sumar { public static void main(String[] args) throws IOException { System.out.println("Please introduce numbers, one per line."); String line; InputStreamReader s = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(s); int sum = 0; line = stdin.readLine(); while (line != null) { sum = sum + Integer.parseInt(line.trim()); line = stdin.readLine(); } System.out.println("The sum of the numbers is " + sum); s.close(); stdin.close(); } }