org.apache.commons.io.FileUtils Example
In this example we are going to show some of the capabilities of the FileUtils
class, which is a part of Apache Commons IO. The methods implemented in this class are all about file manipulation, and in many cases they make a developer’s life much easier. The main reason that this and other classes in Apache Commons exist, is that their methods are wrappers for very usual tasks, so the boilerplate code is immediately smaller and the program as a whole cleaner and more understandable.
1. FileUtils Methods
We will explain the most important methods found inside the FileUtils
class, and then we are going to show how to actually use them. FileUtils
is a static class, which means that we don’t instatiate it, but we are using the class name and the appropriate method instead, like this: FileUtils.method()
.
contentEquals(File file1, File file2)
: It compares the contents of 2 File objects, and returns whether they are the same or not.copyDirectory(File source, File destination)
: It copies a whole directory (source) to another (destination). Bothe need to be opened in Java as file objects.copyDirectory(File source, File destination, FileFilter filter)
: Same as above, but in this case we can filter the contents of a directory by applying specific file filters (checking for names, modification dates, etc).copyFile(File srcFile, File destFile)
: Copies a file to another file.deleteDirectory(File directory)
: Deletes a directory recursively.- getTempDirectory(): Returns a File object that represents the system temporary directory.
getUserDirectory()
: Same as above, but this time representing the user directory.lineIterator(File file)
: It creates an iterator that can iterate over the lines of a given File.sizeOfDirectory(File directory)
: It returns the size of a directory including its contents.write(File file, CharSequence data)
: Writes a CharSequence in a file, using the specified enoding.writeLines(File file, Collection<?> lines)
: Writes the collection of lines to a File.
So, let’s see how these methods are used in action!
2. FileUtils Example
AnnotationsExample.java
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import org.apache.commons.io.FileUtils; import org.apache.commons.io.LineIterator; import org.apache.commons.io.filefilter.SuffixFileFilter; public class FileUtilsExample { private static final String MAIN_PATH = "C:\\Users\\Lilykos\\Desktop\\FUExFiles\\"; public static void main(String[] args) throws IOException { // file1: "Hello World!" // file2: "Hello World!!!" File file1 = FileUtils.getFile(MAIN_PATH + "cmpFile1.txt"); File file2 = FileUtils.getFile(MAIN_PATH + "cmpFile2.txt"); System.out.println("Are cmpFile1 and cmpFile2 equal: " + FileUtils.contentEquals(file1, file2)); // We can copy a whole path somewhere // and check if the operation was successful. FileUtils.copyDirectory( FileUtils.getFile(MAIN_PATH), // source FileUtils.getFile(MAIN_PATH + "copiedPath\\")); // destination System.out.println("Does the copiedPath exist: " + FileUtils.getFile(MAIN_PATH + "copiedPath\\").exists()); // Copy a directory by filtering out all the txt files. // In the directory right now: cmpFile1.txt, cmpFile2.txt, zipFile.zip FileUtils.copyDirectory( FileUtils.getFile(MAIN_PATH), FileUtils.getFile(MAIN_PATH + "copiedFilterPath\\"), new SuffixFileFilter(".zip")); for (File f: FileUtils.getFile(MAIN_PATH + "copiedFilterPath\\").listFiles()) { System.out.println("Contents of copiedFilterPath: " + f.getName()); } // Copy a file File copy = FileUtils.getFile(MAIN_PATH + "copyOfFile1.txt"); FileUtils.copyFile(file1, copy); System.out.println("Are cmpFile1 and copyOfFile1 equal: " + FileUtils.contentEquals(file1, copy)); // Right now there are these files in the MAIN_PATH. for (File f: FileUtils.getFile(MAIN_PATH).listFiles()) { System.out.println("Contents of MAIN_PATH: " + f.getName()); } // Let's delete a directory and see the results. FileUtils.deleteDirectory(FileUtils.getFile(MAIN_PATH + "copiedFilterPath\\")); for (File f: FileUtils.getFile(MAIN_PATH).listFiles()) { System.out.println("Contents of MAIN_PATH after deletion: " + f.getName()); } // Directories System.out.println("Temp Dir: " + FileUtils.getTempDirectory().getAbsolutePath()); System.out.println("User Dir: " + FileUtils.getUserDirectory().getAbsolutePath()); // Line Iterator LineIterator iter = FileUtils.lineIterator(file1); while (iter.hasNext()) { System.out.println("cmpFile1 lines: " + iter.next()); } // Directory size System.out.println("Size of dir: " + FileUtils.sizeOfDirectory(FileUtils.getFile(MAIN_PATH)) + " bytes."); // Ways of writing lines to a file. File fileToWrite1 = FileUtils.getFile(MAIN_PATH + "fileToWrite1.txt"); File fileToWrite2 = FileUtils.getFile(MAIN_PATH + "fileToWrite2.txt"); Collection lines = new ArrayList<>(); lines.add("Written with FileUtils!"); lines.add("For Java Code Geeks."); FileUtils.write(fileToWrite1, "Written with FileUtils!"); FileUtils.writeLines(fileToWrite2, lines); iter = FileUtils.lineIterator(fileToWrite1); while (iter.hasNext()) { System.out.println("fileToWrite1 lines: " + iter.next()); } iter = FileUtils.lineIterator(fileToWrite2); while (iter.hasNext()) { System.out.println("fileToWrite2 lines: " + iter.next()); } } }
Output
Are cmpFile1 and cmpFile2 equal: false Does the copiedPath exist: true Contents of copiedFilterPath: zipFile.zip Are cmpFile1 and copyOfFile1 equal: true Contents of MAIN_PATH: cmpFile1.txt Contents of MAIN_PATH: cmpFile2.txt Contents of MAIN_PATH: copiedFilterPath Contents of MAIN_PATH: copiedPath Contents of MAIN_PATH: copyOfFile1.txt Contents of MAIN_PATH: fileToWrite1.txt Contents of MAIN_PATH: fileToWrite2.txt Contents of MAIN_PATH: zipFile.zip Contents of MAIN_PATH after deletion: cmpFile1.txt Contents of MAIN_PATH after deletion: cmpFile2.txt Contents of MAIN_PATH after deletion: copiedPath Contents of MAIN_PATH after deletion: copyOfFile1.txt Contents of MAIN_PATH after deletion: fileToWrite1.txt Contents of MAIN_PATH after deletion: fileToWrite2.txt Contents of MAIN_PATH after deletion: zipFile.zip Temp Dir: C:\Users\Lilykos\AppData\Local\Temp User Dir: C:\Users\Lilykos cmpFile1 lines: Hello World! Size of dir: 304 bytes. fileToWrite1 lines: Written with FileUtils! fileToWrite2 lines: Written with FileUtils! fileToWrite2 lines: For Java Code Geeks.
3. Download the example
This was an example of FileUtils
in Apache Commons IO.
You can download the full source code of this example here : FileUtilsExample.zip