Question: JAVA PROJECT (PLEASE SHOW SOLUTION) Create a NetBeans project for this activity. Consider the following code. Create a file, name it anything you want and
JAVA PROJECT (PLEASE SHOW SOLUTION)
- Create a NetBeans project for this activity.
Consider the following code. Create a file, name it anything you want and paste the directory on the
import java.io.*;
public class FileIOExercise {
public static void main(String args[]){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(
fileOutputStream = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
}
}
| Test Stem / Question | Choices |
| 1: Try to compile the code. What is wrong with it? | A: There is compile error because of unreported exception. |
| B: There is a syntax error. | |
| C: Compile error because of streams cant be used this way. | |
| D: run perfectly | |
| 2: Try to fix the error. What should you do? | A: Catch the exception or declare a throw statement. |
| B: Rename the local variables. | |
| C: Remove the close statement of the fileOutputStream. Only input streams need to be closed. | |
| D: Nothing | |
| 3: What exceptions do you need to catch or throw when dealing with Java IO?
| A: A, B & C |
| B: Only A & B | |
| C: A Only | |
| D: B only | |
| 4: Now that you have compiled and run the code? What happened? | A: The context of my input is copied from the output.txt |
| B: There a file not found exception since I did not define a output.txt directory. | |
| C: Did not copy my input but no exception was thrown. | |
| D: Compiler error. |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
