Fortunately, there are other classes you can use. That is, based on new topics we have studied so far (classes, construction, inheritance, abstraction, and exception handling), we can consider alternatives to the Scanner class.
To provide support for the ability to read values from a stream (examples of streams are the keyboard or a file), the java.io package provides many classes. One of the main classes in this package is called Reader. Reader is an abstract class that most serves as a foundation for other classes that want to read characters from a stream. Because it is abstract, you cannot declare a variable from the Reader class. Still, it is good to know that this class has two constructors whose syntaxes are: protected Reader(); protected Reader(Object lock); To support the ability to read a byte, the Reader class is equipped with a method named read. This method is overloaded with three versions. Of course, because Reader is abstract, classes derived from it must implement the read() method.
Because Reader is abstract, many classes are derived from it, which allows them to customize their behavior. One of the most valuable operations you can perform on a stream is to read one or a group of characters. To support this operation, one of the classes derived from Reader is called InputStreamReader. In reality, the InputStreamReader class reads a byte and converts it into a character. By doing this repeatedly, it can read a series of bytes and convert that series into a group of characters, which ultimately results into a string. The InputStreamReader class is equipped with two constructors. Their syntaxes are: public InputStreamReader(InputStream in); public InputStreamReader(InputStream in, String enc); Therefore, before starting to read from an input stream, you can declare a variable of type InputStreamReader. Here is an example: import java.io.*; public class Exercise { public static void main(String[] args) { InputStreamReader isr = new InputStreamReader(System.in); } } To provide the ability to read from a stream, the InputStreamReader class implements the read() method that it inherits from Reader. The Reader class provides two versions of the read() method. |
|
||||||||||||||||||
|
After a Reader-based variable has read a byte and produced a character, or after getting one or a series of characters, they are assembled into a buffer. To read from this buffer, you would need another intermediary class. Fortunately, the java.io package provides a class named BufferedReader. The BufferedReader class is derived from the Reader class.
To read from a buffer, you can declare a variable of type BufferedReader. The BufferedReader class is equipped with two constructors. Their syntaxes are: public BufferedReader(Reader in); public BufferedReader(Reader in, int sz); As you can see, when declaring a BufferedReader variable, you must at least pass a Reader-based variable. For example, to read from the keyboard, you can pass a new instance of the InputStreamReader class. Here is an example: import java.io.*; public class Exercise { public static void main(String[] args) { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); } } Because the role of the InputStreamReader variable is to initialize the BufferedReader object, you do not have to first declare an InputStreamReader variable. You can pass the instead of the InputStreamReader class directly to the BufferedReader declaration. This could be done as follows: import java.io.*; public class Exercise { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); } } When using the BufferedReader class, you must indicate that the method where it is used would throw an exception. This would be done as follows: import java.io.*; public class Exercise { public static void main(String[] args) throws Exception { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(isr); } }
One of the operations you can perform with a BufferedReader variable is to read a character. To support this, the BufferedReader class overrides the read() method that it inherits from the Reader class. Among the strengths of the BufferedReader class is its ability to read a line of text. To support this operation, the BufferedReader class is equipped with a method named readLine. Its syntax is: public String readLine(); This method takes no argument. When it is called, it reads a line of text. A line of text is one or a group of characters starting from the left beginning and ends with a new line feed, a carriage return, or a linefeed. After performing its operator, this method produces the string it read. After getting the line that was read, if you want to use it as a string, it is ready. Otherwise, you can convert it to the desired and appropriate value.
|
|
||||||||||||||
|