How to Write Java Code to Delete Several Files at Once

In Java, you may want to write code that will delete several files at once. This can be a little trickier than it sounds. The code to delete the file looks like this:

new File("importantData.txt").delete();

In that code, the new File call refers to a single file. It's very nice code, but it doesn't tell you how to delete a bunch of files. How can you write code to deal with several files at once?

Fortunately, Java provides ways to deal with bunches of files. One way uses an array of File objects.

import java.io.File;
class IHateTxtFiles {
 public static void main(String args[]) {
  File folder = new File(".");
  for (File file : folder.listFiles()) {
   if (file.getName().endsWith(".txt")) {
    file.delete();
   }
  }
 }
}

In many operating systems (including Windows, Mac OS, and Linux), a single dot stands for the current working directory: the place where a program starts looking for files. For a Java program running in Eclipse, this working directory is the project's root directory.

For example, imagine that the code above lives in an Eclipse project named 16-06. Then your hard drive contains a folder named 16-06, which in turn, contains a folder named src; which in turn, contains the IHateTxtFiles.java file. The program's working directory is the 16-06 directory. So, the code

folder = new File(".")

makes folder refer to the directory named 16-06.

image0.jpg

You’re probably thinking: “The project's root directory, 16-06, is a folder, not a file. But the code says folder = new File("."). Why doesn't the code say folder = new Folder(".")?

It turns out that most operating systems blur the differences between folders and files. For Java's purposes, the document IHateTxtFiles.java is a file, the folder named src is also a kind of a file, and the folder named 16-06 is also a kind of a file.

In Java, every File object has a listFiles method, and when you call folder.listFiles(), you get an array. Each “value” stored in the array is one of the files in the folder.

for (TypeName variableName : RangeOfValues) {
Statements
}

The RangeOfValues is an array. The array contains all the files inside the 16-06 project directory. So the enhanced for loop takes each file inside the 16-06 directory and asks “Does this file's name end with .txt?”

if (file.getName().endsWith(".txt"))

If a particular file's name ends with .txt, delete that file:

file.delete();

Before running this example, the 16-06 directory contains things named src, aFile.txt, save.me, and xFile.txt.

image1.jpg

After running this example, the 16-06 directory still contains src and save.me, but no longer contains aFile.txt or xFile.txt.

image2.jpg

After running this program, you might not see any changes in Eclipse's Package Explorer. To verify that the project directory no longer contains .txt files, select the 16-06 branch in the Package Explorer. Then, in Eclipse's main menu, click File→Refresh.

Eclipse's Package Explorer looks like it’s displaying all the files and folders on a part of your hard drive. But looks can be deceiving. Some of the Package Explorer's branches represent neither files nor folders. And some of your hard drive's files and folders don't appear in Eclipse's Package Explorer.

In addition to things like src, which appears in Eclipse's Package Explorer, a project's folder typically contains files named .classpath and .project, and folders named .settings and bin. These additional files and folders aren't normally displayed in Eclipse's Package Explorer.

When you call folder.listFiles(), the resulting array doesn't include any of the things in subdirectories of the folder directory.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com