In: Computer Science
Modify the processFile method so that it will compile and it will not produce a runtime error: public static void processFile(File file) { File input = "contents.txt"; String line = null; try { input = new File(file); while ((line = input.readLine()) != null) { System.out.println(line); } return; } finally { if (file != null) { file.close(); } } }
public static void processFile(File file) {
BufferedReader input =
null;
String line =
null;
try {
input = new BufferedReader(new FileReader(file));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
} catch
(FileNotFoundException ex) {
System.out.println("File not present");
} catch (IOException ex)
{
System.out.println("Cannot read from file");
} catch (Exception
ex){
System.out.println("Somthing went wrong");
} finally {
if (file != null) {
try {
input.close();
} catch (IOException ex) {
System.out.println("File cannot be closed");
} catch (Exception ex){
System.out.println("Somthing went wrong");
}
}
}
}