Filter Streams   «Prev  Next»

Lesson 13Course project, part 2
ObjectiveDump a File as floating point numbers, double-precision floating point numbers, shorts, ints, and longs.

Dump File as floating point numbers in Java

Double-precision floating point numbers, shorts, ints, and longs

Now let us complete the course project. Expand the program developed in the last module so that it also dumps a file as floating point numbers, double-precision floating point numbers, shorts, ints, and longs. You will need to add command line switches for each of these possibilities. For example, use -i as the command line switch for int format. Print each number on a separate line.


Quiz Question regarding BufferedReader

Given the following code (assume appropriate imports):
public class IOTest {
 public static void main(String[] args) {
  Path myfile = Paths.get("test.txt");
  try(BufferedReader bfr = Files.newBufferedReader(myfile, Charset.forName("US-ASCII") )){
   String line = null;
   while( (line = bfr.readLine()) != null){
    System.out.println(line);
   }
  }catch(Exception e){
    System.out.println(e);
  }
 }
}


What will be printed when this code is run if test.txt doesn't exist? (Select 1 option:)
  1. java.io.FileNotFoundException: test.txt
  2. java.nio.file.FileNotFoundException: test.txt
  3. java.nio.file.NoSuchFileException: test.txt
  4. java.nio.file.InvalidPathException : test.txt


Answer: c
Explanation:
c. This exception will be thrown when the program tries to create a BufferedReader to read the file specified by the Path object.
d. This exception is thrown when the argument passed while creating the Path object is invalid. For example, "c:c:test.txt". In the given code, the path string is valid, so this exception will not be thrown. The existence of the file is not checked at the time of creation of Path object.

DumpFile Course Project - Exercise

Click the Exercise link below to modify the course project program you began in the previous module.
DumpFile Course Project - Exercise