Java Reference
In-Depth Information
14. Both read and readLine in the class BufferedReader might throw an IOException .
15. The try block in Display 10.7 is larger so that it includes the invocations of the
method readLine , which might throw an IOException . The method println
in Display 10.1 does not throw any exceptions that must be caught.
16. Yes.
17. No, you must read the number as a string and then convert the string to a number
with Integer.parseInt (or in some other way).
18. When the method readLine tries to read beyond the end of a file, it returns the
value null . Thus, you can test for the end of a file by testing for null .
19. The method read reads a single character , but it returns it as a value of type int .
To get the value to be of type char , you need to do a type cast.
20. When the method read tries to read beyond the end of a file, it returns the
value - 1 . Thus, you can test for the end of a file by testing for the value - 1 . This
works because all “real” characters return a positive int value.
21. Yes, if original.txt is an empty file, then the file numbered.txt produced by
the program will also be empty.
22. Only the classes FileReader and FileOutputStream have a constructor that accepts
a file name as an argument. (Although we have not discussed it, the class Scanner has
a constructor that takes a String argument, but the argument is not a file name.)
23. Yes, it is legal.
24. Replace
System.setErr(errStream);
with
System.setOut(errStream);
25. Add
System.setOut(errStream);
to get
System.setErr(errStream);
System.setOut(errStream);
26. import java.io.File;
public class FileExercise
{
public static void main(String[] args)
{
File fileObject = new File("Sally.txt");
Search WWH ::




Custom Search