$show=/label

Java - How to Delete Files and Folders?

SHARE:

A quick guide to delete the files and folders in java with example programs.

1. Overview

In this tutorial, We will learn how to delete the files and folders in java.

Let us learn the example programs on file deletion and folder removal in java.

Java - How to Delete Files and Folders?


2. Java Files Delete Example

First, Use delete() method on the file object to delete the file. Returns true if the file is delete successfully and else return false if there are any failures.

In the below program, we have taken two files test.log file is present in the location and no-file.log does not exists on the location.

Let us see the behaviour of delete() method.


package com.javaprogramto.files.delete;

import java.io.File;

/**
 * How to delete the file in java using File api delete() method.
 * 
 * @author JavaProgramTo.com
 *
 */
public class FileDelete {

	public static void main(String[] args) {

		// File deletion success
		String fileName = "src/main/java/com/javaprogramto/files/delete/test.log";
		
		File file = new File(fileName);
		
		boolean isFileDeleted = file.delete();
		
		if(isFileDeleted) {
			System.out.println("File deleted without any errors for "+fileName);
		} else {
			System.out.println("File deletion is failed");
		}
		
		// File deletion error.
		
		fileName = "src/main/java/com/javaprogramto/files/delete/no-file.log";
		
		file = new File(fileName);
		
		isFileDeleted = file.delete();
		
		if(isFileDeleted) {
			System.out.println("File deleted without any errors for "+fileName);
		} else {
			System.out.println("File deletion is failed for "+fileName);
		}


	}

}
 

Output:

File deleted without any errors for src/main/java/com/javaprogramto/files/delete/test.log
File deletion is failed for src/main/java/com/javaprogramto/files/delete/no-file.log

 

3. Java Delete Folder Example

Next, we will try to delete the folder which is having the files and next empty folder deletion using delete() method.


package com.javaprogramto.files.delete;

import java.io.File;

/**
 * How to delete the folder in java using File API delete() method.
 * 
 * @author JavaProgramTo.com
 *
 */
public class FileDeleteFolder {

	public static void main(String[] args) {

		// Folder deletion not done
		String folderName = "src/main/java/com/javaprogramto/files/delete";
		
		File file = new File(folderName);
		
		boolean isFileDeleted = file.delete();
		
		if(isFileDeleted) {
			System.out.println("Folder with files is deleted");
		} else {
			System.out.println("Folder with files is not deleted");
		}
		
		// Empty Folder deletion success .
		
		folderName = "src/main/java/com/javaprogramto/files/emptyfolder";
		
		file = new File(folderName);
		
		isFileDeleted = file.delete();
		
		if(isFileDeleted) {
			System.out.println("Empty Folder deleted ");
		} else {
			System.out.println("Empty Folder deletion is failed for "+folderName);
		}
	}
}
 

Output:

Folder with files is not deleted
Empty Folder deleted 

 

Note: if the folder is empty then only folder will be deleted and folder which has files won't be deleted. But, we can delete the files folder after deleting all files.

4. Conclusion

In this article, we've seen how to delete the files and folder in java with examples.

GitHub

How to compress and decompress the files in java?

File.delete() API

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java - How to Delete Files and Folders?
Java - How to Delete Files and Folders?
A quick guide to delete the files and folders in java with example programs.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhx57sFSb4oJ7Tt7usNLhuBrcTWTOuD8jhtq2qfhCpc3xgD9pFaWJffY8kbXy3io8dUcqwKCk4NHi01bY8wNKU39fUoyaCJlpD03iOKkfb0B5WRqG1_hKmDEF-OecGBCG19Ovynv5jQCSg/w400-h246/Java+-+How+to+Delete+Files+and+Folders%253F.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhx57sFSb4oJ7Tt7usNLhuBrcTWTOuD8jhtq2qfhCpc3xgD9pFaWJffY8kbXy3io8dUcqwKCk4NHi01bY8wNKU39fUoyaCJlpD03iOKkfb0B5WRqG1_hKmDEF-OecGBCG19Ovynv5jQCSg/s72-w400-c-h246/Java+-+How+to+Delete+Files+and+Folders%253F.png
JavaProgramTo.com
https://www.javaprogramto.com/2021/03/java-how-to-delete-files.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2021/03/java-how-to-delete-files.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content