CS-FreeStyle / 10000-How-To-Do-in-CS

1 stars 0 forks source link

java new, Some input files use unchecked or unsafe operations. #114

Open liuty10 opened 4 years ago

liuty10 commented 4 years ago

error: unreported exception FileNotFoundException; must be caught or declared to be thrown PrintWriter myWriter = new PrintWriter("/tmp/vncwin.cfg"); ^ Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error

Using try.. catch to check possible exceptions: import java.io.FileWriter; // Import the FileWriter class import java.io.IOException; // Import the IOException class to handle errors https://www.w3schools.com/java/java_files_create.asp

public class WriteToFile { public static void main(String[] args) { try { FileWriter myWriter = new FileWriter("filename.txt"); myWriter.write("Files in Java might be tricky, but it is fun enough!"); myWriter.close(); System.out.println("Successfully wrote to the file."); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } }