Java Commons IO Tutorial

1- Instroduction

2- Library

3- Create Project

Create Maven Project.
  • Group ID: org.o7planning
  • Artifact ID: CommonsIOTutorial
This is a image after project was created.
Configuring Maven to use Commons IO:
  • pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.o7planning</groupId>
    <artifactId>CommonsIOTutorial</artifactId>
    <version>0.0.1-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    </dependencies>

</project>

4- IOUtils

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();
        }
    }
}

 

5- FileUtils

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 really no 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();
        }
    }

}

6- FilenameUtils

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:
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.

7- 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.
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();
        }
    }
}

 

8- 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();
        }
    }
}

 

9- File filters

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
  • FileFilterExample2.java
  • 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]);
        }
    }
}

 
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());
        }

    }
}

 

10- File comparators

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

 

11- Streams

The org.apache.commons.io.input and org.apache.commons.io.output packages contain various useful implementations of streams. These include: