next up previous
Next: File I/O Up: I/O, I/O, it's off Previous: I/O, I/O, it's off

Keyboard & Display I/O

  1. Recall that the String class is in package java.lang
  2. Going to look at System class (java.lang) and BufferedReader class (java.io)
  3. nothing special to do to use classes in java.lang, but must put import java.io.*; at beginning of file for classes in package java.io

Schematic of the a computer system that is running a Java program looks like this:

Figure 17: Simplified schematic of computer system
\begin{figure}
\begin{center}
\epsfxsize =5in {\epsfbox {system.ps}} \\
\end{center}\end{figure}

Java program has to talk through all these layers to hardware in order to read from the keyboard or write to the display. I/O is a little more complicated than previous constructions:


import java.io.*; 
class Test { 

    private static BufferedReader in; 

    public static void main(String[] args) throws IOException { 
            InputStreamReader k = new InputStreamReader(System.in); 
            in = new BufferedReader(k); 
            String il = in.readLine(); 
            System.out.println("il = " + il); 
        } 
}

What does this do?

See the corrections to the lecture I gave on this topic.

More coming in this section: stuff about parseInt, parseDouble, what InputStreamReader and BufferedReader do, a brief explanation of exception handling



Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09