CHAPTER 10_ FILE IO EXERCISE 1- 45 _ ONE PACKAGE
EXERCISE 1- 45 _ ONE PACKAGE Exercise 1: A stream is a flow of data. From where and to where does the data flow in an input stream? From where and to where does the data flow in an output stream? Exercise 2: What is the difference between a binary file and a text file? Exercise 3What kind of exception might be thrown by the following, and what would it indicate if this exception is thrown?PrintWriter outputStream = new PrintWriter(new FileOutputStream("stuff.txt")); Exercise 4 Does the class PrintWriter have a constructor that accepts a string (for a file name) as an argument, so that the following code would be legal? PrintWriter outputStream = new PrintWriter("stuff.txt"); Exercise 5 Write some code that will create a stream named outStream that is a member of the class PrintWriter, and that connects this stream to a text file named sam so that your program can send output to the file. Do this so that the file sam always starts out empty. So, if there already is a file named sam, the old contents of sam are lost. Exercise 6 As in Self-Test Exercise 5, write some code that will create a stream named outStream that is a member of the class PrintWriter, and that connects this stream to a text file named sam so that your program can send output to the file. This time, however, do it in such a way that, if the file sam already exists, the old contents of sam will not be lost and the program output will be written after the old contents of the file. Exercise 7: The class Person was defined in Display 5.19 of Chapter 5. Suppose mary is an object of the class Person, which has a toString method defined, and suppose outputStream is connected to a text file as in Display 10.1. Will the following send sensible output to the file connected to outputStream? outputStream.println(mary); Exercise 8: Write some code that will create a stream named fileIn that is a member of the class Scanner. It should connect the stream to a text file named sally so that your program can read input from the text file sally. Exercise 9:Might the method nextInt in the class Scanner throw an exception? If so, what type of exception? Exercise 10:If the method hasNextInt returns false, does that mean that reading has reached the end of the file? Exercise 11:Might the following throw an exception that needs to be caught or declared in a throws clause?Scanner inputStream = new Scanner(new FileInputStream("morestuff.txt")); (The stream inputStream would be used to read from the text file morestuff.txt.) Exercise 12:Write some code that will create a stream named fileIn that is a member of the class BufferedReader and that connects the stream to a text filenamed joe so that your program can read input from the text file joe. 13 -Exercise 21: Does the program in Display 10.9 work correctly if original.txt is an empty file? The program is as follows: .......Exercise 38 Suppose a binary file contains three numbers written to the file with the method writeDouble of the class ObjectOutputStream. Suppose further that your program reads all three numbers with three invocations of the method readDouble of the class ObjectInputStream. When will an EOFException be thrown? Right after reading the third number? When your program tries to read a fourth number? Some other time? Exercise 45 How can you modify the program in Display 10.22 so the file always starts out empty?
You'll get a 23.9KB .JAVA file.