Java Commons IO Tutorial with Examples
1. Library
Maven Dependency
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
2. IOUtils
No ADS
IOUtils is a utility class to assist in processing the stream more quickly. If you use standard java.io you can do the things you want. But want to code faster, save time, use IOUtils.
See Javadoc of IOUtils here:
Take a look at the code to be processed in the usual way (Using what is available in JDK).
InputStream in = new URL("http://commons.apache.org").openStream();
try {
InputStreamReader inR = new InputStreamReader(in);
BufferedReader buf = new BufferedReader(inR);
String line;
while ((line = buf.readLine()) != null) {
System.out.println(line);
}
} finally {
in.close();
}
And here is the code used IOUtils class:
InputStream in = new URL("http://commons.apache.org").openStream();
try {
System.out.println(IOUtils.toString(in));
} finally {
IOUtils.closeQuietly(in);
}
Looking at the two above code we can see clearly the same as a goal, but use IOUtils much faster .
ReadURL1.java
package org.o7planning.tutorial.commonsio.io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class ReadURL1 {
public static void readURL() throws MalformedURLException, IOException {
InputStream in = new URL("http://commons.apache.org").openStream();
try {
InputStreamReader inR = new InputStreamReader(in);
BufferedReader buf = new BufferedReader(inR);
String line;
while ((line = buf.readLine()) != null) {
System.out.println(line);
}
} finally {
in.close();
}
}
public static void main(String[] args) {
try {
readURL();
} catch (Exception e) {
e.printStackTrace();
}
}
}
ReadURL2.java
package org.o7planning.tutorial.commonsio.io;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.IOUtils;
public class ReadURL2 {
public static void readURL() throws MalformedURLException, IOException {
InputStream in = new URL("http://commons.apache.org").openStream();
try {
System.out.println(IOUtils.toString(in));
} finally {
IOUtils.closeQuietly(in);
}
}
public static void main(String[] args) {
try {
readURL();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. FileUtils
No ADS
The FileUtils class contains utility methods for working with File objects. These include reading, writing, copying and comparing files.
Javadoc FileUtils:
FileUtilsReadFile.java
package org.o7planning.tutorial.commonsio.fileutils;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class FileUtilsReadFile {
public static void readFile() throws IOException {
File file= new File("D:/test.txt");
List<String> lines= FileUtils.readLines(file);
for(String s: lines) {
System.out.println(s);
}
}
public static void main(String[] args) {
try {
readFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileUtilsTouchFile.java
package org.o7planning.tutorial.commonsio.fileutils;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import java.io.File;
public class FileUtilsTouchFile {
public static void main(String[] args) {
try {
// E:/test not exist on disk
File testFile = new File("E:/test.txt");
// Get last modify time
long fileTimestamp = testFile.lastModified();
System.out.println("Time in milis " + fileTimestamp);
// Using the touch() operation
// If file not exists, it create file.
// It change the file date and time stamps
FileUtils.touch(testFile);
System.out.println("Time in milis updated "
+ testFile.lastModified());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Running for the first time (E:/test.txt does not exists on hard drive)
Running second (When E:/test.txt has been created and update last modify date)
Use FileUtils allows you to easily copy and paste files from a certain place to another place, or copy the files from the folder it to another folder.
FileUtilsCopyDirectory.java
package org.o7planning.tutorial.commonsio.fileutils;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class FileUtilsCopyDirectory {
public static void main(String[] args) {
File srcDir = new File("C:/test");
File destDir = new File("D:/test/abc");
try {
FileUtils.copyDirectory(srcDir, destDir);
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. FilenameUtils
No ADS
The FilenameUtils class contains utility methods for working with filenames without using File objects. The class aims to be consistent between Unix and Windows, to aid transitions between these environments (such as moving from development to production).
FilenameUtils javadoc:
For example to normalize a filename removing double dot segments:
FilenameUtilsNormalize.java
package org.o7planning.tutorial.commonsio.filenameutils;
import org.apache.commons.io.FilenameUtils;
public class FilenameUtilsNormalize {
public static void main(String[] args) {
String filename = "C:/commons/io/../lang/project.xml";
String normalized = FilenameUtils.normalize(filename);
System.out.println(normalized);
}
}
Run and get results:
FilenameUtils has more than 40 utility method, you can see detail in the Javadoc.
5. FileSystemUtils
The FileSystemUtils class contains utility methods for working with the file system to access functionality not supported by the JDK. Currently, the only method is to get the free space on a drive. Note that this uses the command line, not native code.
FileSystemUtilsFreeSpaceKb.java
package org.o7planning.tutorial.commonsio.filesystemutils;
import java.io.IOException;
import org.apache.commons.io.FileSystemUtils;
public class FileSystemUtilsFreeSpaceKb {
public static void main(String[] args) {
try {
Long kb = FileSystemUtils.freeSpaceKb("C:/");
System.out.println("Free Space: " + kb + "KB");
} catch (IOException e) {
e.printStackTrace();
}
}
}
6. Line iterator
The org.apache.commons.io.LineIterator class provides a flexible way for working with a line-based file. An instance can be created directly, or via factory methods on FileUtils or IOUtils. The recommended usage pattern is:
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
// / do something with line
}
} finally {
LineIterator.closeQuietly(it);
}
LineIteratorExample.java
package org.o7planning.tutorial.commonsio.lineiterator;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
public class LineIteratorExample {
public static void main(String[] args) {
try {
File file = new File("D:/test.txt");
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
//
if (line != null && line.startsWith("##")) {
System.out.println(line.substring(2));
}
}
} finally {
LineIterator.closeQuietly(it);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
7. File filters
No ADS
The org.apache.commons.io.filefilter package defines an interface (IOFileFilter) that combines both java.io.FileFilter and java.io.FilenameFilter. Besides that the package offers a series of ready-to-use implementations of the IOFileFilter interface including implementation that allow you to combine other such filters (FileFilterUtils). These filters can be used to list files or in FileDialog
For example, java.io.FileFilter can be used to filter the file you want in one specific folder. But it would be better if you use the class in org.apache.commons.io.filefilter package. It has pre-written for you, and just use.
Let's look at an example not using org.apache.commons.io.filefilter
FileFilterExample1.java
package org.o7planning.tutorial.commonsio.filefilter;
import java.io.File;
import java.io.FileFilter;
public class FileFilterExample1 {
public static void main(String[] args) {
FileFilter filter = new MyFileFilter();
File dir = new File("C:/test");
// Get file with extension js or css.
File[] list = dir.listFiles(filter);
for (File file : list) {
System.out.println("File " + file.getAbsolutePath());
}
}
// Filter accept files (*.js , *.css)
static class MyFileFilter implements FileFilter {
public boolean accept(File pathname) {
String abstractPath = pathname.getAbsolutePath();
if (abstractPath.endsWith(".js") || abstractPath.endsWith(".css")) {
return true;
}
return false;
}
}
}
And for example, use SuffixFileFilter class, located in package org.apache.commons.io.filefilter
HiddenFileFilterExample.java
package org.o7planning.tutorial.commonsio.filefilter;
import java.io.File;
import org.apache.commons.io.filefilter.HiddenFileFilter;
public class HiddenFileFilterExample {
public static void main(String[] args) {
File dir = new File("C:/test");
String[] files = dir.list(HiddenFileFilter.HIDDEN);
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
}
}
FileFilterExample2.java
package org.o7planning.tutorial.commonsio.filefilter;
import java.io.File;
import java.io.FileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
public class FileFilterExample2 {
public static void main(String[] args) {
String[] suffixs = new String[] { ".js", ".css" };
FileFilter filter = new SuffixFileFilter(suffixs);
File dir = new File("C:/test");
// Get file with extension js or css.
File[] list = dir.listFiles(filter);
for (File file : list) {
System.out.println("File " + file.getAbsolutePath());
}
}
}
For example, combining two filters together:
CombineTwoFilterExample.java
package org.o7planning.tutorial.commonsio.filefilter;
import java.io.File;
import java.io.FileFilter;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.HiddenFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
public class CombineTwoFilterExample {
public static void main(String[] args) {
String[] suffixs = new String[] { ".js", ".css" };
IOFileFilter filter1 = new SuffixFileFilter(suffixs);
//
IOFileFilter filter2 = HiddenFileFilter.VISIBLE;
//
// Filter accept file with extension (js,css) and Visible.
IOFileFilter newFilter = FileFilterUtils
.andFileFilter(filter1, filter2);
File dir = new File("C:/test");
File[] list = dir.listFiles((FileFilter) newFilter);
for (File file : list) {
System.out.println("File " + file.getAbsolutePath());
}
}
}
8. File comparators
No ADS
The org.apache.commons.io.comparator package provides a number of java.util.Comparator implementations for java.io.File. These comparators can be used to sort lists and arrays of files
SizeFileComparatorExample.java
package org.o7planning.tutorial.commonsio.comparator;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.comparator.SizeFileComparator;
public class SizeFileComparatorExample {
public static void main(String[] args) throws IOException {
// Current directory
File directory = new File(".");
File[] files = directory.listFiles();
System.out.println("Default order");
displayFiles(files);
Arrays.sort(files, SizeFileComparator.SIZE_COMPARATOR);
System.out
.println("\nSizeFileComparator.SIZE_COMPARATOR (Ascending, directories treated as 0)");
displayFiles(files);
Arrays.sort(files, SizeFileComparator.SIZE_REVERSE);
System.out
.println("\nSizeFileComparator.SIZE_REVERSE (Descending, directories treated as 0)");
displayFiles(files);
Arrays.sort(files, SizeFileComparator.SIZE_SUMDIR_COMPARATOR);
System.out
.println("\nSizeFileComparator.SIZE_SUMDIR_COMPARATOR (Ascending, directory size used)");
displayFilesWithDirectorySizes(files);
Arrays.sort(files, SizeFileComparator.SIZE_SUMDIR_REVERSE);
System.out
.println("\nSizeFileComparator.SIZE_SUMDIR_REVERSE (Descending, directory size used)");
displayFilesWithDirectorySizes(files);
}
public static void displayFiles(File[] files) {
for (File file : files) {
System.out.printf("%-20s Size:" + file.length() + "\n",
file.getName());
}
}
public static void displayFilesWithDirectorySizes(File[] files) {
for (File file : files) {
if (file.isDirectory()) {
System.out.printf(
"%-20s Size:" + FileUtils.sizeOfDirectory(file) + "\n",
file.getName());
} else {
System.out.printf("%-20s Size:" + file.length() + "\n",
file.getName());
}
}
}
}
Results run class SizeFileComparatorExample:
Default order
.classpath Size:1431
.project Size:569
.settings Size:4096
pom.xml Size:547
src Size:0
target Size:0
test.txt Size:0
SizeFileComparator.SIZE_COMPARATOR (Ascending, directories treated as 0)
.settings Size:4096
src Size:0
target Size:0
test.txt Size:0
pom.xml Size:547
.project Size:569
.classpath Size:1431
SizeFileComparator.SIZE_REVERSE (Descending, directories treated as 0)
.classpath Size:1431
.project Size:569
pom.xml Size:547
.settings Size:4096
src Size:0
target Size:0
test.txt Size:0
SizeFileComparator.SIZE_SUMDIR_COMPARATOR (Ascending, directory size used)
test.txt Size:0
.settings Size:489
pom.xml Size:547
.project Size:569
.classpath Size:1431
src Size:11307
target Size:22433
SizeFileComparator.SIZE_SUMDIR_REVERSE (Descending, directory size used)
target Size:22433
src Size:11307
.classpath Size:1431
.project Size:569
pom.xml Size:547
.settings Size:489
test.txt Size:0
No ADS
Java Basic
- Data Types in java
- Java PhantomReference Tutorial with Examples
- JDK Javadoc in CHM format
- Java Stream Tutorial with Examples
- Java Predicate Tutorial with Examples
- Java BiConsumer Tutorial with Examples
- Arrays in Java
- JDBC Driver Libraries for different types of database in Java
- Abstract class and Interface in Java
- Java Commons Email Tutorial with Examples
- Install Eclipse
- Bitwise Operations
- Install Eclipse on Ubuntu
- Configuring Eclipse to use the JDK instead of JRE
- Java Commons Logging Tutorial with Examples
- Java Enums Tutorial with Examples
- Loops in Java
- Java Regular Expressions Tutorial with Examples
- Install Java on Ubuntu
- Quick Learning Java for beginners
- Install Java on Windows
- Comparing and Sorting in Java
- Inheritance and polymorphism in Java
- Java Consumer Tutorial with Examples
- Java String, StringBuffer and StringBuilder Tutorial with Examples
- Java Exception Handling Tutorial with Examples
- Example of Java encoding and decoding using Apache Base64
- if else statement in java
- Switch Statement in Java
- Java Supplier Tutorial with Examples
- Java Programming for team using Eclipse and SVN
- Java JDBC Tutorial with Examples
- Java remote method invocation - Java RMI Tutorial with Examples
- Java Multithreading Programming Tutorial with Examples
- Customize java compiler processing your Annotation (Annotation Processing Tool)
- What is needed to get started with Java?
- Java Aspect Oriented Programming with AspectJ (AOP)
- Understanding Java System.identityHashCode, Object.hashCode and Object.equals
- Java Compression and Decompression Tutorial with Examples
- Java Reflection Tutorial with Examples
- Install OpenJDK on Ubuntu
- Java String.format() and printf() methods
- History of Java and the difference between Oracle JDK and OpenJDK
- Introduction to the Raspberry Pi
- Java Socket Programming Tutorial with Examples
- Java Generics Tutorial with Examples
- Manipulating files and directories in Java
- Java WeakReference Tutorial with Examples
- Java Commons IO Tutorial with Examples
- History of bits and bytes in computer science
- Which Platform Should You Choose for Developing Java Desktop Applications?
- Java SoftReference Tutorial with Examples
- Syntax and new features in Java 8
- Java Annotations Tutorial with Examples
- Java Function Tutorial with Examples
- Access modifiers in Java
- Java BiFunction Tutorial with Examples
- Get the values of the columns automatically increment when Insert a record using JDBC
- Java Functional Interface Tutorial with Examples
- Java BiPredicate Tutorial with Examples
Show More
- Java Servlet/Jsp Tutorials
- Java Collections Framework Tutorials
- Java API for HTML & XML
- Java IO Tutorials
- Java Date Time Tutorials
- Spring Boot Tutorials
- Maven Tutorials
- Gradle Tutorials
- Java Web Services Tutorials
- Java SWT Tutorials
- JavaFX Tutorials
- Java Oracle ADF Tutorials
- Struts2 Framework Tutorials
- Spring Cloud Tutorials