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.
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:)
- java.io.FileNotFoundException: test.txt
- java.nio.file.FileNotFoundException: test.txt
- java.nio.file.NoSuchFileException: test.txt
- 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