18.8 | Why do you have to declare to throw IOException in the method or use a try-catch block to handle IOException for Java IO programs? |
18.9 | Why should you always close streams? |
18.10 | InputStream reads bytes. Why does the read() method return an int instead of a byte? Find the abstract methods in InputSteam and OutputStream . |
18.11 | Does FileInputStream / FileOutputStream introduce any new methods? How do you create a FileInputStream / FileOutputStream ? |
[Page 631] |
18.12 | What will happen if you attempt to create an input stream on a nonexistent file? What will happen if you attempt to create an output stream on an existing file? Can you append data to an existing file? |
18.13 | How do you append data to an existing text file using java.io.PrintWriter ? |
18.14 | Suppose input is a DataInputStream , input.available() returns 100. After invoking read() , what is input.available() ? After invoking readInt() , what is input.available() ? After invoking readChar() , what is input.available() ? After invoking readDouble() , what is input.available() ? |
18.15 | What is written to a file using writeByte(91) on a FileOutputStream ? |
18.16 | How do you check the end of a file in a binary input stream ( FileInputStream , DataInputStream )? |
18.17 | What is wrong in the following code? import java.io.*; public class Test { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream( "test.dat" ); } catch (IOException ex) { ex.printStackTrace(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } } } |
18.18 | Suppose you run the program on Windows using the default ASCII encoding. After the program is finished, how many bytes are in the file t.txt ? Show the contents of each byte. public class Test { public static void main(String[] args) throws java.io.IOException { java.io.PrintWriter output = new java.io.PrintWriter( "t.txt" ); output.printf( "%s" , "1234" ); output.printf( "%s" , "5678" ); output.close(); } } |
18.19 | After the program is finished, how many bytes are in the file t.dat ? Show the contents of each byte. import java.io.*; public class Test { public static void main(String[] args) throws IOException { DataOutputStream output = new DataOutputStream( new FileOutputStream( "t.dat" )); output.writeInt( 1234 ); output.writeInt( 5678 ); output.close(); } } |
[Page 632] |
18.20 | For each of the following statements on a DataOutputStream out , how many bytes are sent to the output? output.writeChar( 'A' ); output.writeChars( "BC" ); output.writeUTF( "DEF" ); |
18.21 | What are the advantages of using buffered streams? Are the following statements correct? BufferedInputStream input1 = new BufferedInputStream( new FileInputStream( "t.dat" )); DataInputStream input2 = new DataInputStream( new BufferedInputStream( new FileInputStream( "t.dat" ))); ObjectInputStream input3 = new ObjectInputStream( new BufferedInputStream( new FileInputStream( "t.dat" ))); |