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,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- [fix] fatal: pathspec index.html did not match any files error - Git
- [Fix] Java Exception with Lambda - Cannot invoke because object is null - Java
- Mac - Steam Needs to Be Online to Update. Please confirm your network connection and try again error - News
- Find MAC address of your laptop device - HowTos
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin - Java
- PowerShell Switch Statement with Examples - Powershell
- Add Text at Start and End of Each Line Notepad++ - NotepadPlusPlus
- Remove Html head and body tags from ckeditor source - Html