Input/Output
![]()
The BufferedReader class is used for reading in input.
Processing Streams
Processing streams do reading and writing. They perform some sort of operation, such as buffering or character encoding, as they read and write.In this section, we want to focus on the input process. The table below gives some java.io's processing streams which are related to standard input.
Process CharacterStreams Buffering BufferedReader,
...Converting between
Bytes and CharactersInputStreamReader,
...The processing streams are briefly described here:
- BufferedReader
- Buffer data while reading or writing, thereby reducing the number of accesses required on the original data source. Buffered streams are typically more efficient than similar nonbuffered streams.
InputStreamReader
It forms the bridge between byte streams and character streams. An InputStreamReader reads bytes from an InputStream and converts them to characters using either the default character-encoding or a character-encoding specified by name. (You don't need to know about character-encoding right now. It has something to do with representing characters in different languages, such as English, French, Chinese, etc.)
![]()
- Character Streams
Streams that read and write 16-bit characters.
- Byte Streams
Programs should use the byte streams, descendants of InputStream and OutputStream, to read and write 8-bit bytes. InputStream and OutputStream provide the API and some implementation for input streams (streams that read 8-bit bytes) and output streams (streams that write 8-bit bytes). These streams are typically used to read and write binary data such as images and sounds.
Keyboard Input Handling
As shown above, we would like to process keyboard input in the following way
- The InputStream object System.in takes keyboard input, and put the data in Java byte format.
- An InputStreamReader object takes the data, translate it from bytes to characters.
- An BufferReader object buffers the character inputs, and allows the computer program to process the data a section at a time.
How to program this in Java?
1. We need to declare an object of the class BufferedReaderBufferedReader in;
in = new BufferedReader (new InputStreamReader(System.in));
We have explained why we need System.in, InputStreamReader object and BufferedReader object in the figure above
2. Input can fail. We need to warn the compiler:public static void main (String[] args) throws IOException
{ //... }3. Not every program does input. We need to ask for the input class library:
import java.io.*; //before "public class ..."
4. Now we're ready for input. All we get is Strings!
String inputLine;
inputLine = in.readLine();BufferedReader's readlLine method reads one line of input.
To convert the String to an integer (type int), we need a method of the class Integer:
int inputInt;
inputInt = Integer.parseInt (inputLine);The method parseInt throws a NumberFormatException if the format of args[0] isn't valid. (You don't need to know what this Exception is right now. It is just a warning message indicating that the String you are parsing does not look like an integer.) All of the Number classes--Integer, Float, Double, and so on--have parseXXX methods that convert a String representing a number to an object of their type. (We will talk about these number classes in later lectures.)
Example program: Read and print one integer
//-------------------------
// EchoInt: read and print one integer
//-------------------------import java.io.*;
public class EchoInt
{
public static void main (String[] args) throws IOException
{
System.out.print("Enter a number here -- ");
BufferedReader in = new BufferedReader
(new InputStreamReader(System.in));
String line = in.readLine();
int i = Integer.parseInt(line);
System.out.println("You entered " + i);
}
}