import java.io.IOException;
      import java.nio.file.*;
      
      try {
        Files.deleteIfExists(Paths.get("filepath"));
      }
      catch(NoSuchFileException e)
      {
        // file does not exists
      }
      catch(DirectoryNotEmptyException e)
      {
        // attempt to delete a directory that is not empty
      }
      catch(IOException e)
      {
        // other issues (such as permissions)
      }
      Java language logo

      Delete a file in Java

      file

      Delete a file or a directory in Java. Directory must not be empty.

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.