Lesson 4.1: Advanced Programming Logic Flashcards

1
Q

What is an I/O stream?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What two types of streams do modern versions of Java define?

A

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When are byte streams used over character streams?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When are character streams used over byte streams?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What do InputStream and OutputStream define?

A

InputStream defines the characteristics common to byte input streams and OutputStream describes the behavior of byte output streams.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the two class hierarchy abstract classes that character streams are topped by?

A

Character streams are defined by using two class hierarchies topped by these two abstract classes: Reader and Writer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which one is which for input and output: reader and writer

A

Reader is used for input، and Writer is used for output. Concrete classes derived from Reader and Writer operate on Unicode character streams.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the several concrete subclasses that are derived from Reader and Writer?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

By default، what do System.out، System.in، and System.err refer to?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What system commands are Input stream and which are output stream?

A

System.in is an object of type InputStream; System.out and System.err are objects of type PrintStream.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Name 3 (out of the many) InputStream methods and 3 OutputStream methods.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

For commercial coding، what is the preferred stream orientation? Why

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the three different versions of read()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does int read( ) throws IOException do?

A

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.

17
Q

What does int read(byte data[ ]) throws IOException do?

A

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.

18
Q

What does int read(byte data[ ]، int start، int max) throws IOException do?

A

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.

19
Q

print() and println() are methods of what class stream?

A

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).

20
Q

What is the simplest form of write()?

A

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.

21
Q

What are some of the methods in the abstract classes Reader and Writer?

A

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.

22
Q

What is the best class for reading console input and when can you use it directly with System.in?

A

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.

23
Q

What does int read( ) throws IOException int read(char data[ ]) throws IOException int read(char data[ ]، int start، int max) throws IOException do?

A

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.

24
Q

What is the general form of readLine()?

A

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

25
Q

Give me a code example that uses BufferedReader and the readLine() method.

A

The following program demonstrates BufferedReader and the readLine( ) method. The program reads and displays lines of text until you enter the word “stop”.

26
Q

Why is it preferred to use PrintWriter over System.out in large scale programs?

A

using a character-based class for console output makes it easier to internationalize your program.

27
Q

What are two commonly used FileWriter constructors?

A

FileWriter creates a Writer that you can use to write to a file. Two commonly used constructors are shown here: FileWriter(String fileName) throws IOException FileWriter(String fileName، boolean append) throws IOException Here، fileName is the full path name of a file. If append is true، then output is appended to the end of the file. Otherwise، the file is overwritten. Either throws an IOException on failure. FileWriter is derived from OutputStreamWriter and Writer. Thus، it has access to the methods defined by these classes.

28
Q

Give me a code that uses key to disk utility and writes lines of text to a txt file.

A

Here is a simple key-to-disk utility that reads lines of text entered at the keyboard and writes them to a file called “test.txt”. Text is read until the user enters the word “stop”. It uses a FileWriter to output to the file.

29
Q

What does a FileReader class do? What is a commonly used constructor for a FileReader?

A

The FileReader class creates a Reader that you can use to read the contents of a file. A commonly used constructor is shown here: FileReader(String fileName) throws FileNotFoundException Here، fileName is the full path name of a file. It throws a FileNotFoundException if the file does not exist.

30
Q

Write a code that reads a text file using simple disk-to-screen utility.

A

The following program creates a simple disk-to-screen utility that reads a text file called “test.txt” and displays its contents on the screen. Thus، it is the complement of the key-to-disk utility shown in the previous section.

31
Q

What is NIO

A

Originally called New I/O، NIO was added to Java by JDK 1.4. It supports a channel-based approach to I/O operations. The NIO classes are contained in java.nio and its subordinate packages، such as java.nio.channels and java.nio.charset.

32
Q

What is NIO.2?

A

Beginning with JDK 7، NIO was substantially enhanced، so much so that the term NIO.2 is often used. The improvements included three new packages (java.nio.file، java.nio.file.attribute، and java.nio.file.spi); several new classes، interfaces، and methods; and direct support for stream-based I/O. The additions greatly expanded the ways in which NIO can be used، especially with files.

33
Q

Why are type wrappers needed?

A

Java’s type wrappers are classes that encapsulate، or wrap، the primitive types. Type wrappers are needed because the primitive types are not objects.

34
Q

What are the type wrappers?

A

The type wrappers are Double، Float، Long، Integer، Short، Byte، Character، and Boolean. These classes offer a wide array of methods that allow you to fully integrate the primitive types into Java’s object hierarchy. As a side benefit، the numeric wrappers also define methods that convert a numeric string into its corresponding binary equivalent.

35
Q

Try This 10-1: A File Comparison Utility

A

Try This 10-1 A File Comparison Utility

36
Q

Try This 10-2: Creating a Disk-Based Help System

A

Try This 10-2 Creating a Disk-Based Help System