Standard I/O with System Class Example

// ... reading characters from the keyboard ... try { // IOException is possible // Input bytes from the keyboard and print to the terminal for(int aByte = 0; ( aByte = System.in.read() ) != -1 ; ) System.out.print((char)aByte); } catch (IOException ioe) { // print the exception onto the err stream System.err.println("Reading from the keyboard error:" + ioe); }

// SysRead import java.io.*; /** * Example of reading String and integer from the keyboard * using System.in stream * @@author Jeff Zhuk */ public class SysRead { public static void main(String[] args) { // invitation to enter a number System.out.println("Please enter an integer number:"); // get the number String inputString = null; int number = 0; try { BufferedInputStream in = new BufferedInputStream(System.in); inputString = new String( in.read() ); // bytes > String in.close(); number = Integer.parseInt(inputString); } catch(Exception e) { System.out.println("Exception: " + e); System.exit(0); } System.out.println("Thank you for the right input: " + number); } }

Back to I/O Classes Overview