Question
Correct the code: 2. import java.io.File; // Import the File class import java.io.FileNotFoundException; // Import this class to handle errors import java.util.Scanner; // Import the
Correct the code:
2. import java.io.File; // Import the File class import java.io.FileNotFoundException; // Import this class to handle errors import java.util.Scanner; // Import the Scanner class to read text files public class ReadFile { public static void main(String[] args) { myObj = new File("filename.txt"); Scanner myReader = Scanner(myObj); while (myReader.hasNextLine()) { String data =nextLine(); System.out.println(data); } myReader.close(); catch (e) { System.out.println("An error occurred."); e.printStackTrace(); } } } Output: Files in Java might be tricky, but it is fun enough!
3. public class DeleteFile { public static void main(String[] args) { File = new File(); if (myObj.delete()) { System.out.println("Deleted the file: " + myObj.getName()); } else { System.out.println("Failed to delete the file."); } } Output: Deleted the file: filename.txt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started