In this post we will provide an example on how to delete a file in Java. This example requires no additional libraries we will use the builtin features of Java to delete a file.
How to delete a file in Java
Path path = Paths.get("<Path of file to delete>");
File file = path.toFile();
The above example shows you need to do the following:
- Construct the path object for the file you wish to delete.
- Then convert it to a File object
- Remove file if exists
- How to create a file in Java
- How to read a file in Java
- List files in a directory
- how to write to a file in Java