This post shows you how to use Java to delete files and directories. There are 4 ways to delete files and folders on Windows. After Java deletes files that exist, you can also retrieve them via EaseUS Data Recovery Wizard.
You might occasionally run into a file you can't delete normally. In that case, you must first understand the cause if you want to solve the issue. Usually, the main cause is that the file is encrypted or locked. If not, a virus needs to be considered.
Don't worry. You can use Java to delete file if exists as long as you know Java language. Even if you are a novice to Java programs, you can quickly get the 4 methods mentioned in this article.
What is Java? Sun Microsystems introduced Java in 1995, a programming language and computing platform. It has grown from its modest origins to power a significant portion of today's digital world by offering the solid foundation upon which numerous services and applications are developed. Java is still used in cutting-edge goods and digital services that are being developed for the future.
Besides, you can use Java to set PC instructions that tell your computers how to perform different tasks. When you want to delete specific files, For instance:
⚠️You can use Java to delete links, directories, and files. With symbolic links, the link itself is removed rather than the target. As for directories, the deletion must succeed with an empty directory. |
Before learning the 4 ways, you could watch the following tutorial video first. The instructions for Java to delete files by running specific scripts are included in the video.
If you are still confused, please read the 4 solutions below.
When using the conventional File IO java.io.*, you can delete a file using the File.delete( ) method returns a boolean value (true if the file was successfully deleted, false otherwise).
To delete files in Java, try the following command:
import java.io.File;
public class DeleteFileExample1 {
public static void main(String[] args) {
// File to be deleted
File file = new File("foo.txt");
// Delete file
boolean isDeleted = file.delete();
if(isDeleted) {
System.out.println("File deleted successfully");
} else {
System.out.println("File doesn't exist");
}
}
}
How to Use CMD Delete File/Folder [Windows 11]
Command Prompt is a command-line interpreter application also known as cmd.exe or cmd. It can only be used in Windows operating system. Read more >>
The delete(Path) method either successfully deletes the file or throws an error if it cannot. For instance, a NoSuchFileException is raised if the file does not exist. To find out why the delete failed, you can capture the exception as follows:
try {
Files.delete(path);
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
System.err.format("%s not empty%n", path);
} catch (IOException x) {
// File permission problems are caught here.
System.err.println(x);
}
You can also delete a folder and its subfolders. However, the folders must be empty:
import java.io.File;
public class DeleteFolder {
public static void main(String[] args) {
File myObj = new File("C:\\Users\\MyName\\Test");
if (myObj.delete()) {
System.out.println("Deleted the folder: " + myObj.getName());
} else {
System.out.println("Failed to delete the folder.");
}
}
}
How to Force Delete a Folder or File in Windows 10/Windows 11
You can force delete folders or files in CMD, use force delete folder software, change ownership, or force delete folder in Safe Mode. Read more >>
To delete the directory, firstly, you should provide the path of the directory and prepare to delete all the files and subfolders.
import java.io.File;
class DeleteDirectory {
// function to delete subdirectories and files
public static void deleteDirectory(File file)
{
// store all the paths of files and folders present
// inside directory
for (File subfile : file.listFiles()) {
// if it is a subfolder,e.g Rohan and Ritik,
// recursively call function to empty subfolder
if (subfile.isDirectory()) {
deleteDirectory(subfile);
}
// delete files and empty subfolders
subfile.delete();
}
}
How to Delete Files, Folders, and Directories in Linux Using Command
How do I delete a file in Linux using Terminal? This article explains how to remove files, folders, or directories in Linux Terminal. Read more >>
We have learned 4 different ways to use Java to delete files and directories. After that, don't forget to mark this passage by sharing it on Facebook, Twitter, or SNS.
If you were trying the scripts above but accidentally deleted some system files or folders, you might have removed the files necessary for Java commands. If that happens, you ought to recover deleted files from Windows 10.
You may also unintentionally delete your important private files and folders and have no idea of how to restore permanently deleted items. Fortunately, using the EaseUS Data Recovery Wizard, you may quickly return these necessary items.
Check the steps and learn how to use the software to recover deleted files:
Step 1. Select the location to scan
Choose the specific device and drive where you have permanently deleted files using Shift delete or emptying recycle bin. Then, click the "Scan" button to find lost files.
Step 2. Check the results
The software will automatically start scanning all over the selected drive. When the scan completes, select the "Deleted Files" and "Other Lost Files" folders in the left panel. Then, apply the "Filter" feature or click the "Search files or folders" button to quickly find the deleted files.
Step 3. Recover deleted files
Select the deleted files and click "Preview". Next, click "Recover" to save them to another secure location or device.
This article offers you four workable solutions. Most users claim that their issues were resolved after using Method 1. And Method 1 is the one I prefer most. If you have a different solution to this issue that you would want to share with us, please do so; our readers would be very interested in reading it.
Additionally, your PC might function effectively to safeguard your files even deleted by Java with EaseUS Data Recovery Wizard.
Here are 4 additional questions about Java delete files. Check here to see if it has the question you are interested in and find the answers.
1. How to delete a file in Java?
Using the File. delete( ) function of the File class; we can delete a file in Java. The file or directory indicated by the abstract pathname is deleted by the delete( ) method. If the pathname is a directory, it must be empty to be deleted.
2. How to force delete folder Windows open in another program?
You can: To avoid getting the File in Use error when removing folders:
3. How to delete files older than X days in Windows 11/10 using Storage Sense?
4. How do I delete multiple files in the command prompt?
The del command allows you to delete individual and group files.
Related Articles
Recover Deleted Chrome Downloads and Resume Canceled Chrome Downloads
Fix You Don't Currently Have Permission to Access This Folder
How to Restore IE Edge Favorites/Bookmarks? | IE Edge Recovery
How to Recover Deleted Items in Outlook for Windows(2024)