• First Steps in Java Input and Output

    First, here is a simple Hello World program to illustrate basic I/O.
        To output to screen:
    	// prints new line character after text
    	System.out.println("text_here" + "more_text_here") 
    
    	// does not print a new line character after text
    	System.out.print("text_here" + "more_text_here") 
    
    	// for a more sophisticated "formatted printing",
    	// 	the C-style printf
    	//	is available.
    
        Input is more complicated.
    	The method System.in.read()
    	reads one character at a time, too primitive for
    	most users.  Perhaps the simplest is to use
    	readStream objects. 
    	CLICK HERE for more information.
    
        What follows are mostly deprecated stuff!!
    
    	To read an input (deprecated version):
    
    	// Be sure to import java.io.*.
    	DataInputStream in = new DataInputStream(System.in);
    	String answer;
    
    	System.out.print("Quit? (Type "Y" or "y")");
    	System.out.flush();
    
    	response = in.readLine();
    	if(!response.equals("Y") && !response.equals("y"))
    		...
    
    There are two problems with the input code above:
    (a) DataInputStream() has been deprecated and
    (b) you ought to catch the exception that may be thrown by readLine().
    The fix for (a) is to replace DataInputStream by BufferedReader, as illustrated here: The old version
    DataInputStream d = new DataInputStream(in);
    
    should become:
    BufferedReader d = new BufferedReader(new InputStreamReader(in));
    
    Here is the program illustrating the above code. Unfortunately, this does not compile in Microsoft world. Also notice how the two Hello World Programs represent two different ways of catching IO exceptions.

    Link to Sun's I/O Tutorial:

    --Overview
    --I/O Essentials
    -- How to Use DataInputStream and DataOutputStream
    -- Reading from a URL