Delete file in java


I'm doing an agenda in java for the uni :) and I do not get that in one of the options that I have of menu that is to erase the data.txt was deleted I have tried couple of things that have happened to me and seen many tutorials but I do not get this has been the last code tried I hope you can help me

public void clearConsole() {

    File fichero = new File("data.txt");

    if (fichero.delete())
        System.out.println("El fichero ha sido borrado satisfactoriamente");
        System.out.println("El fichero no pudó ser borrado");
asked by Flakotenorio 16.12.2016 в 02:33

5 answers


The code you use is fine, you probably have problems with privileges or permissions. Try doing it with administrator permissions.

It is also possible that you have the file path incorrectly placed.

answered by 16.12.2016 в 11:33

You can create this function:

public static void eliminarFichero(File fichero) {

    if (!fichero.exists()) {
        System.out.println("El archivo data no existe.");
    } else {
        System.out.println("El archivo data fue eliminado.");


And use it later

public static void main(String[] args) {

    File fichero = new File("RUTA_DEL_ARCHIVO");

answered by 16.12.2016 в 04:29

Delete a file in Java

To delete a file we must invoke the delete () method of the File class.

Example, delete the file "example1.txt":


public class EliminarArchivo {

    public static void main(String args[]){


            File archivo = new File("C:\carpeta1\ejemplo1.txt");

            boolean estatus = archivo.delete();;

            if (!estatus) {

                System.out.println("Error no se ha podido eliminar el  archivo");


                System.out.println("Se ha eliminado el archivo exitosamente");


        }catch(Exception e){






See here

answered by 16.12.2016 в 08:15

You can use delete (Path) , where you define the path of the file or directory to be deleted.

try {
} 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.

You can also use deleteIfExists (Path) , the difference is that this method also deletes the file, but if the file does not exist, no exception occurs.



This is what the user (OP) has, which is incorrect, because the method does not exist delete () :

 File fichero = new File("data.txt");

but instead there is the method delete (Path path) , which I expose in my answer.

I make the clarification regarding the person who gave me a negative score and I add a comment, surely you have not read the entire thread since it exists delete (Path path) .

answered by 19.02.2017 в 21:38

It has been 2 years since the question was raised but I want to contribute the following: If the file is being used by another process such as in a "FileInputStream" it is necessary to first close it before trying to delete it with the method " close() " Example:

File imagen = new File("C:\ejemplo.jpg");
FileInputStream readImage = new FileInputStream(imagen);

If I try to use at this point " imagen.delete() " I will not get anything since it is being used by InputStream so it is necessary: p>


This should delete the file correctly.

answered by 06.12.2018 в 00:30