File deletion
- Eclipse: Oxygen
- Java: 1.8
The concept of file deletion is shown here. This is done by a simple command of file.delete() method.
The Java File delete() method can be used to delete files, it will return a boolean value. It returns true if the file is deleted; false if failed.
In the following program, we are using the File.delete() method. It will delete txt file named “studyeasy\\team.txt”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.io.File; public class Main { public static void main(String[] args) { File file = new File("studyeasy\\team.txt"); if(file.delete()){ System.out.println("Deletion is successful"); }else{ System.out.println("Error while deleting"); } } } |
Output
1 |
Deletion is successful |
Contributed by: Poonam Tomar