Java IO - Remove a File

May 16, 2020 No comments Java IO Remove File

1. Introduction

In this article, we are going to present several ways to delete files in Java using JDK 6, 7 and Apache Commons IO library. Removing files is common functionality often used in applications to clean up not used resources.

This article is a part of the Java IO Tutorial.

2. Delete the file using JDK 6

Starting with the old school way:

package com.frontbackend.java.io.remove;

import java.io.File;
import java.io.IOException;

public class RemoveFileUsingFileObject {

    public static void main(String[] args) throws IOException {
        File file = new File("/tmp/toremove.txt");
        if (file.delete()) {
            System.out.println("File successfully deleted");
        } else {
            System.out.println("File not deleted, probably it does not exist");
        }
    }
}

In this example, we created an instance of File object and made use of its delete() method. The method returns true if delete operation was successful, otherwise false. We will get the negative result when a file doesn't exist.

3. Removing files in JDK 7

In JDK 7 the modern Files class was introduced. The following example shows how to use it:

package com.frontbackend.java.io.remove;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class RemoveFileUsingFilesObject {

    public static void main(String[] args) throws IOException {
        File file = new File("/tmp/toremove.txt");
        Files.delete(file.toPath());
    }
}

If the file does not exist, Files.delete(...) method will throw an exception NoSuchFileException. So, in this case, we have a different than in the JDK 7 approach to handle not existed files.

Exception in thread "main" java.nio.file.NoSuchFileException: /tmp/toremove.txt

4. Using Apache Commons IO library to remove files

Using external libraries to the simple operations on files might be not the best solution but it is doable and gives us additional options and features:

package com.frontbackend.java.io.remove;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class RemoveFileUsingFileUtils {

    public static void main(String[] args) throws IOException {
        File file = FileUtils.getFile("/tmp/toremove.txt");
        boolean success = FileUtils.deleteQuietly(file);

        if (success) {
            System.out.println("File successfully deleted");
        } else {
            System.out.println("File not deleted, probably it does not exist");
        }
    }
}

In this example, we made use of FilesUtils.deleteQuietly(...) method that simply removes the file and returns true if the file was removed successfully.

In case we do want an exception to be throw, we can use FilesUtils.forceDelete(...) method:

package com.frontbackend.java.io.remove;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class RemoveFileUsingFileUtils {

    public static void main(String[] args) throws IOException {
        File file = FileUtils.getFile("/tmp/toremove.txt");
        FileUtils.forceDelete(file);
    }
}

5. Conclusion

In this article, we illustrated various ways to delete a file in Java. This article covered methods used in JDK 6, 7 and presented in Apache Commons IO library.

Code snippets used in this article are available under the GitHub repository.

{{ message }}

{{ 'Comments are closed.' | trans }}