In this article, we take a look at how to delete a file using Java with examples.
Exampl 1: Using java.io.File delete() method
package org.code2care.examples;
import java.io.File;
public class DeleteFile {
public static void main(String[] args) {
String fileName = "/Users/c2ctech/Desktop/data.csv";
File dataFile = new File(fileName);
if (dataFile.delete()) {
System.out.println("File " + dataFile.getName() + " deleted successfully!");
} else {
System.out.println("Unable to delete file " + dataFile.getName());
}
}
}
Example 2: Using java.io.File delete()
package org.code2care.examples;
import java.io.File;
public class DeleteFile {
public static void main(String[] args) {
String fileName = "/Users/c2ctech/Desktop/data.csv";
File dataFile = new File(fileName);
if (dataFile.exists()) {
if (dataFile.delete()) {
System.out.println("File " + dataFile.getName() + " deleted successfully!");
} else {
System.out.println("Unable to delete file " + dataFile.getName());
}
} else {
System.out.println("File " + dataFile.getName() + " does not exist.");
}
}
}
Example 3: Using java.nio.file.File
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DeleteFile {
public static void main(String[] args) {
String fileName = "/Users/c2ctech/Desktop/data.csv";
Path filePath = Paths.get(fileName);
try {
Files.delete(filePath);
System.out.println("File " + fileName + " deleted successfully!");
} catch (Exception e) {
System.out.println("Unable to delete file " + fileName);
e.printStackTrace();
}
}
}
Example 4: Using java.nio.file.Path
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DeleteFile {
public static void main(String[] args) {
String fileName = "/Users/c2ctech/Desktop/data.csv";
Path filePath = Paths.get(fileName);
try {
Files.deleteIfExists(filePath);
System.out.println("File " + fileName + " deleted successfully!");
} catch (Exception e) {
System.out.println("Unable to delete file " + fileName);
e.printStackTrace();
}
}
}

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Fix: This account is currently not available. (Linux SSH) - Linux
- How to create new user account in Windows bash - Bash
- cURL: Show Request and Response Headers - cURL
- Rust: zsh: no such file or directory: ./main - Rust
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020 - Android
- How to add a Task List to Quick View Menu in SharePoint Online Site - SharePoint
- Program 4: Input Name and Age and Print - 1000+ Python Programs - Python-Programs
- How to Change the Default Shell in Windows Terminal App - Windows