/* Requirement: We need a program that compares the contents of two files f1.txt and f2.txt and output the comparison result "Same" or "Different". Each file should have only one line of text. The program should use proper exception handling to handle the following problems: (1) if f1.txt cannot be open: output the message "Cannot open f1" (2) if f2.txt cannot be open: output the message "Cannot open f2" (3) if f1.txt has no content: output the message "f1 is empty. " (4) if f2.txt has no content: output the message "f2 is empty. " Note: If any problem happens, the program should output the above message and stop; it should not proceed and should not output comparison result "Same" or "Different". */ // Peter's program import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner f1=null, f2=null; String content1=null, content2=null; try { f1 = new Scanner(new File("f1.txt")); f2 = new Scanner(new File("f2.txt")); } catch (FileNotFoundException e) { if (f1==null) { System.out.println("Cannot open f1"); } else { System.out.println("Cannot open f2"); } } try { content1 = f1.nextLine(); content2 = f2.nextLine(); } catch (NoSuchElementException e) { //no content in file if (content1==null) { System.out.println("f1 is empty."); } else { System.out.println("f2 is empty."); } } if (content1.equals(content2)) { System.out.println("Same"); } else { System.out.println("Different"); } f1.close(); f2.close(); } }