Delete folders and files in java

How to delete a directory in Java

In this article, you’ll learn about different ways to delete a directory in Java. We shall look at examples to delete both an empty and a non-empty directory structure.

In Java 7 and above, you can use Files.delete() from NIO API to easily delete an empty directory:

try  // directory path Path path = Paths.get("./tmp"); // delete directory Files.delete(path); > catch (IOException ex)  ex.printStackTrace(); > 

The above code will throw an exception if the directory doesn’t exist. To avoid the exception, you can use Files.deleteIfExists() that only deletes the directory if it exists:

try  // directory path Path path = Paths.get("./tmp"); // delete directory Files.deleteIfExists(path); > catch (IOException ex)  ex.printStackTrace(); > 

Usually, we are required to delete a directory recursively. Both Files.delete() and Files.deleteIfExists() will throw an exception if the directory is not empty. To delete a non-empty directory, we can use the Files.walk() method to list all files and sub-directories and then delete them one by one as shown below:

try  // create a stream StreamPath> files = Files.walk(Paths.get("./tmp")); // delete directory including files and sub-folders files.sorted(Comparator.reverseOrder()) .map(Path::toFile) .forEach(File::deleteOnExit); // close the stream files.close(); > catch (IOException ex)  ex.printStackTrace(); > 

To delete an empty directory, we can also use the File.delete() method from the Java legacy I/O package:

// directory path File file = new File("./tmp"); // delete directory file.delete(); 

If the directory is not empty, we have to do a little extra work and recursively delete all files and sub-folders as shown below:

public void deleteDir(File dir)  File[] files = dir.listFiles(); if(files != null)  for (final File file : files)  deleteDir(file); > > dir.delete(); > 
File file = new File("./tmp"); deleteDir(file); 

The Apache Commons IO library provides FileUtils.deleteDirectory() method to delete a directory including all files and sub-directories. Here is an example:

try  // directory path File file = new File("./tmp"); // delete directory FileUtils.deleteDirectory(file); > catch (IOException ex)  ex.printStackTrace(); > 
dependency> groupId>commons-iogroupId> artifactId>commons-ioartifactId> version>2.6version> dependency> 
implementation 'commons-io:commons-io:2.6' 

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like.

Источник

How to delete a directory with files in Java — Example

Deleting an empty directory is easy in Java, just use the delete() method of java.io.File class, but deleting a directory with files is unfortunately not easy. You just can’t delete a folder if it contains files or sub folders. Calling delete() method on a File instance representing a non-empty directory will just return false without removing the directory. In order to delete this folder, you need to delete all files and subdirectories inside this folder. This may seem cumbersome, but unfortunately, there is no method that can delete a directory with files in Java, not even on Java 7 Files and Paths class.

So there are two choices, either you write your own method to recursively delete all files and folder before deleting a directory, or alternatively, you can use an open-source utility library like Apache Commons IO which will do this for you.

BTW, If you have to do this without using any third party library then you can use the example shown in this tutorial. You know what, I have asked this question couple of times to Java developers and only 3 out of 10 knows that you cannot delete a directory with files in Java.

I am not surprised because this is the kind of detail which is not obvious. Until you do it, you don’t know this. Java isn’t able to delete folders with data in it. You have to delete all files before deleting the folder, as shown in the first example of this tutorial. Java 7 got much better with files and directory support but there also, unfortunately, no direct method to delete a directory with files. Though, In JDK 7 you could use Files.walkFileTree() and Files.deleteIfExists() to delete a tree of files.

Java Program to delete non empty directory in Java

Primary method to delete a file or directory in Java was File.delete() method form java.io package. This method can be used to delete a file or a nonempty directory but fail silently when it comes to deleting folder with files. If you ignore return value of this method, which is false if it failed to delete directory then you will never know that whether your program failed to remove some directory and I have seen many developers hit by this bullet. Thankfully, JDK 7 added another delete() method in Files utility class to delete file and directory, which throws IOException when a file cannot be deleted. This is really useful for troubleshooting purpose e.g. to find out why a file cannot be deleted. There is one more similar method, Files.deleteIfExists() , which is slightly more readable than original delete() method from File class.

Now coming back to our original question, how to delete a folder with files or sub folder inside it. Well, we need to write a program which can recursively check all files and folder and delete them before deleting the top level directory. In this example, I have create a directory structure with one directory containing a file and a sub-directory containing another file and tried to delete the top directory using our method.

In this program, I have two overloaded method, deleteDirectory(File file) and deleteDirectory(String path) to demonstrate the right and wrong way to delete a directory in Java. The method which accepts a String argument, first print all contents of directory without printing contents of sub directory and then just call delete method on the directory we want to delete. This method cannot remove a non-empty directory, so it failed and return false which is captured in method. The right way to delete a directory with files is to recursively delete any sub directory and files inside it, which is demonstrated in deleteDirectory(File) method.

This method first check if the given File instance represent a directory, if yes then it list all files and sub directory and recursively call the same method to delete each of them. This allows program to remove files inside a sub folder before removing it. Once everything inside given directory is deleted, it proceed to delete the given folder.

import java.io.File; /** * Java Program to delete directory with sub directories and files on it * In this example, we have a directory tree as one/ abc.txt * two/ cde.txt and we will try to delete top level directory one here. * * @author Javin Paul */ public class FileDeleteDemo < public static void main(String args[]) < deleteDirectory("one"); // wrong way to remove a directory in Java deleteDirectory(new File("one")); //right way to remove directory in Java > /* * Right way to delete a non empty directory in Java */ public static boolean deleteDirectory(File dir) < if (dir.isDirectory()) < File[] children = dir.listFiles(); for (int i = 0; i  children.length; i++) < boolean success = deleteDirectory(children[i]); if (!success) < return false; > > > // either file or an empty directory System.out.println("removing file or directory : " + dir.getName()); return dir.delete(); > /* * Incorrect way to delete a directory in Java */ public static void deleteDirectory(String file) < File directory = new File(file); File[] children = directory.listFiles(); for (File child : children) < System.out.println(child.getAbsolutePath()); > // let's delete this directory // it will not work because directory has sub-directory // which has files inside it. // In order to delete a directory, // you need to first delete its files or contents. boolean result = directory.delete(); if (result) < System.out.printf("Directory '%s' is successfully deleted", directory.getAbsolutePath()); > else < System.out.printf("Failed to delete directory '%s' %n", directory.getAbsolutePath()); > > > D:\Programs\Test\one\abc.txt D:\Programs\Test\one\two Failed to delete directory 'D:\Programs\Test\one' removing file or directory : abc.txt removing file or directory : cde.txt removing file or directory : two removing file or directory : one

That’s all on how to delete non-empty directory with files in Java. As you can see in output the method which takes String path is not able to delete our directory with files «one», instead it failed, while our second method has recursively deleted everything inside this top-level directory, you can see files are deleted before directory to make them empty. I have not tested this code heavily but it should work on all Java versions starting from Java 1.2

  • How to read File in one line in Java 8? (example)
  • How to read Microsoft XLS file in Java? (example)
  • How to work with RandomAccessFile in Java? (demo)
  • How to create File and Directory in Java? (solution)
  • How to read/write text files in Java? (solution)
  • How to read/write Properties files in Java? (example)
  • How to read File line by line in Java using BufferedReader? (example)
  • How to use Memory Mapped File in Java? (code)
  • How to make hidden file in Java? (program)
  • How to copy File in Java? (solution)
  • How to check File Permission in Java? (program)
  • Difference between getPath(), getCannonicalPath() and getAbsolutePath() in Java? (answer)
  • How to change File Permission in Java? (solution)
  • Right way to Read Zip Files in Java (example)
  • How to check hidden file in Java? (solution)

Источник

How to Delete Files and Directories in Java

Last updated: 01 June 2020 To delete a file in Java, we can use the delete() method from Files class. We can also use the delete() method on an object which is an instance of the File class. Example:

Deleting a File Using the Files class

import java.io.IOException; import java.nio.file.*; public class DeleteFile < public static void main(String[] args) < Path path = FileSystems.getDefault().getPath("./src/test/resources/newFile.txt"); try < Files.delete(path); >catch (NoSuchFileException x) < System.err.format("%s: no such" + " file or directory%n", path); >catch (IOException x) < System.err.println(x); >> > 

The above code deletes a file named newFile.txt in ./src/test/resources/ directory. The multiple catch() blocks will catch any errors thrown when deleting the file.

Deleting a File Using the File Class

Instead of using the delete() method on the Files class, we can also use the delete() method on an object which is an instance of the File class. Example:

import java.io.File; public class DeleteFile < public static void main(String[] args) < File myFile = new File("./src/test/resources/newFile.txt"); if (myFile.delete()) < System.out.println("Deleted the file: " + myFile.getName()); >else < System.out.println("Failed to delete the file."); >> > 

Delete a File if Exists

import java.io.IOException; import java.nio.file.*; public class DeleteFile < public static void main(String[] args) < Path path = FileSystems.getDefault().getPath("./src/test/resources/newFile.txt"); try < Files.deleteIfExists(path); >catch (IOException x) < System.err.println(x); >> > 

Delete a Directory

We can use the above code to delete a folder as well. If the folder is not empty a DirectoryNotEmptyException is thrown, so we have to explicitly catch the exception.

import java.io.IOException; import java.nio.file.*; public class DeleteFile < public static void main(String[] args) < Path path = FileSystems.getDefault().getPath("./src/test/resources"); try < Files.deleteIfExists(path); >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) < System.err.println(x); >> > 

Источник

Читайте также:  Чем проверять php файлы
Оцените статью