21.3. Declaring Generic Classes and Interfaces

 
[Page 630 ( continued )]

Review Questions

Sections 18.1 “18.2

18.1 What is a text file, and what is a binary file? Can you view a text file or a binary file using a text editor?
18.2 How do you read or write data in Java? What is a stream?

Section 18.3 Text I/O vs. Binary I/O

18.3 What are the differences between text I/O and binary I/O?
18.4 How is a Java character represented in the memory, and how is a character represented in a text file?
18.5 If you write string "ABC" to an ASCII text file, what values are stored in the file?
18.6 If you write string "100" to an ASCII text file, what values are stored in the file? If you write a numeric byte-type value 100 using binary I/O, what values are stored in the file?
18.7 What is the encoding scheme for representing a character in a Java program? By default, what is the encoding scheme for a text file on Windows?

Section 18.4 Binary I/O Classes

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"   ))); 

Section 18.6 Object I/O

18.22 What types of objects can be stored using the ObjectOutputStream ? What is the method for writing an object? What is the method for reading an object? What is the return type of the method that reads an object from ObjectInputStream ?
18.23 If you serialize two objects of the same type, will they take same space?
18.24 Is it true that any instance of java.io.Serializable can be successfully serialized? Are the static variables in an object serialized? How do you mark an instance variable not to be serialized?
18.25 Can you write an array to an ObjectOutputStream ?
18.26 Is it true that DataInputStream / DataOutputStream can always be replaced by ObjectInputStream / ObjectOutputStream ?
18.27 What will happen when you attempt to run the following code?
   import   java.io.*;   public class   Test {   public static void   main(String[] args)   throws   IOException {     ObjectOutputStream output =   new   ObjectOutputStream(   new   FileOutputStream(   "object.dat"   ));       output.writeObject(   new   A());   } }   class   A   implements   Serializable {   B b =   new   B(); }   class   B { } 

Section 18.7 (Optional) Random Access Files

18.28 Can RandomAccessFile streams read and write a data file created by DataOutputStream ? Can RandomAccessFile streams read and write objects?
18.29 Create a RandomAccessFile stream for the file address.dat to allow the updating of student information in the file. Create a DataOutputStream for the file address.dat . Explain the differences between these two statements.

[Page 633]
18.30 What happens if the file test.dat does not exist when you attempt to compile and run the following code?
   import   java.io.*;   public class   Test {   public static void   main(String[] args) {   try   {       RandomAccessFile raf =   new   RandomAccessFile(   "test.dat"   ,   "r"   );   int   i = raf.readInt();     }   catch   (IOException ex) {       System.out.println(   "IO exception"   );     }   } } 

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net