Lesson 4.1: Advanced Programming Logic Flashcards
What is an I/O stream?
An I/O stream is an abstraction that either produces or consumes information. A stream is linked to a physical device by the Java I/O system. All streams behave in the same manner، even if the actual physical devices they are linked to differ.
What two types of streams do modern versions of Java define?
Modern versions of Java define two types of I/O streams: byte and character. (The original version of Java defined only the byte stream، but character streams were quickly added.)
When are byte streams used over character streams?
Byte streams provide a convenient means for handling input and output of bytes. They are used، for example، when reading or writing binary data. They are especially helpful when working with files.
When are character streams used over byte streams?
Character streams are designed for handling the input and output of characters. They use Unicode and، therefore، can be internationalized. Also، in some cases، character streams are more efficient than byte streams.
At the most basic level، which stream is the only stream used? Why do we still use the other if we only (ultimatley) use both?
At the lowest level، all I/O is still byte-oriented. The character-based streams simply provide a convenient and efficient means for handling characters.
What do InputStream and OutputStream define?
InputStream defines the characteristics common to byte input streams and OutputStream describes the behavior of byte output streams.
What are the byte stream classes by name. Since there are so many، here is a hint: BI،BO،AI،AO،DS،DO،FI،FO،LI،LO، etc. Dont expect to memorize them
The byte stream classes in java.io are shown in Table 10-1. Don’t be overwhelmed by the number of different classes. Once you can use one byte stream، the others are easy to master.
What are the two class hierarchy abstract classes that character streams are topped by?
Character streams are defined by using two class hierarchies topped by these two abstract classes: Reader and Writer.
Which one is which for input and output: reader and writer
Reader is used for input، and Writer is used for output. Concrete classes derived from Reader and Writer operate on Unicode character streams.
What are the several concrete subclasses that are derived from Reader and Writer?
From Reader and Writer are derived several concrete subclasses that handle various I/O situations. In general، the character-based classes parallel the byte-based classes. The character stream classes in java.io are shown in Table 10-2.
By default، what do System.out، System.in، and System.err refer to?
System.out refers to the standard output stream. By default، this is the console. System.in refers to standard input، which is by default the keyboard. System.err refers to the standard error stream، which is also the console by default. However، these streams can be redirected to any compatible I/O device.
What system commands are Input stream and which are output stream?
System.in is an object of type InputStream; System.out and System.err are objects of type PrintStream.
Name 3 (out of the many) InputStream methods and 3 OutputStream methods.
At the top of the byte stream hierarchy are the InputStream and OutputStream classes. Table 10-3 shows the methods in InputStream، and Table 10-4 shows the methods in OutputStream.
For commercial coding، what is the preferred stream orientation? Why
For commercial code، the preferred method of reading console input is to use a character-oriented stream. Doing so makes your program easier to internationalize and easier to maintain.
What are the three different versions of read()
There are three versions of read( )، which are shown here: int read( ) throws IOException int read(byte data[ ]) throws IOException int read(byte data[ ]، int start، int max) throws IOException
What does int read( ) throws IOException do?
In Chapter 3، you saw how to use the first version of read( ) to read a single character from the keyboard (from System.in). It returns –1 when an attempt is made to read at the end of the stream.
What does int read(byte data[ ]) throws IOException do?
The second version reads bytes from the input stream and puts them into data until either the array is full، the end of stream is reached، or an error occurs. It returns the number of bytes read، or –1 when an attempt is made to read at the end of the stream.
What does int read(byte data[ ]، int start، int max) throws IOException do?
The third version reads input into data beginning at the location specified by start. Up to max bytes are stored. It returns the number of bytes read، or –1 when an attempt is made to read at the end of the stream.
print() and println() are methods of what class stream?
Console output is most easily accomplished with print( ) and println( )، with which you are already familiar. These methods are defined by the class PrintStream (which is the type of the object referenced by System.out).
What is the simplest form of write()?
it is possible to write to the console by using write( ). The simplest form of write( ) defined by PrintStream is shown here: void write(int byteval) This method writes the byte specified by byteval to the file. Although byteval is declared as an integer، only the low-order 8 bits are written.
What are some of the methods in the abstract classes Reader and Writer?
At the top of the character stream hierarchy are the abstract classes Reader and Writer. Table 10-7 shows the methods in Reader، and Table 10-8 shows the methods in Writer. Most of the methods can throw an IOException on error.
What is the best class for reading console input and when can you use it directly with System.in?
The best class for reading console input is BufferedReader، which supports a buffered input stream. However، you cannot construct a BufferedReader directly from System.in. Instead، you must first convert it into a character stream. To do this، you will use InputStreamReader، which converts bytes to characters. To obtain an InputStreamReader object that is linked to System.in، use the constructor shown next: InputStreamReader(InputStream inputStream) Next، using the object produced by InputStreamReader، construct a BufferedReader using the constructor shown here: BufferedReader(Reader inputReader) Putting it all together، the following line of code creates a BufferedReader that is connected to the keyboard.
What does int read( ) throws IOException int read(char data[ ]) throws IOException int read(char data[ ]، int start، int max) throws IOException do?
int read( ) throws IOException int read(char data[ ]) throws IOException int read(char data[ ]، int start، int max) throws IOException The first version of read( ) reads a single Unicode character. It returns –1 when an attempt is made to read at the end of the stream. The second version reads characters from the input stream and puts them into data until either the array is full، the end of stream is reached، or an error occurs. It returns the number of characters read or –1 when an attempt is made to read at the end of the stream. The third version reads input into data beginning at the location specified by start. Up to max characters are stored. It returns the number of characters read or –1 when an attempt is made to read at the end of the stream.
What is the general form of readLine()?
To read a string from the keyboard، use the version of readLine( ) that is a member of the BufferedReader class. Its general form is shown here: String readLine( ) throws IOException