Using Apache Commons IO FileUtils for File Manipulation

31 Oct
2007

In this post I will show you how we can use Apache Commons-IO FileUtils class to help you doing file manipulation and getting some information about files. To start you need to have the latest commons-io library which you can download from http://jakarta.apache.org/commons project website.

You are now have a handfull classes of Apache Commons-IO. Let’s begin, first I’ll show you how we can read a file content straight to an instance of string using FileUtils class methods, either FileUtils.readFileToString(File file) or FileUtils.readFileToString(File file, String encoding).

I’m using the first method to create an example to show you how to read the content of a file. Let say that we are going to read the content of sample.txt file. So here is the code.

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

public class FileUtilsSample
{
  public static void main(String[] args)
  {
    File file = new File("sample.txt");
    try
    {
      String content = FileUtils.readFileToString(file);
      System.out.println(content);
    } catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

As the readFileToString(File file) is a static method you need to called it by prefixing the method call with its class name. It’s a very basic rule in Java, you should already know about it. We create an instance of File which will be passed into the method. This method also thrown an IOException exception when the operation failed, so we need to add a try-catch block for the method.

The next method is also quite usefull, say that you want to read the file line by line just like the BufferedReader does for you, but one again no while-loop routine. You can do this by using FileUtils.readLines(File file) method. This method read the file content line by line and returns it as a List of string.

Let me give you a simple example to use this method, the example is exactly the same with the previous one but we are using the readLine(File file) method in turn.

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;\r\n\r\npublic class FileUtilsSample
{
  public static void main(String[] args)
  {
    File file = new File("sample.txt");
    try
    {
      List contents = FileUtils.readLines(file);
      for (String line : contents)
      {
        System.out.println(line);
      }
    } catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

I’ve shown you the file reading utilities above, now what about writing to a file, can I use this class to? Yes, our course you can. Maybe you can guess the name of a method that you can used to write to a file. I think I heard you, did you say writeStringToFile? Yes, that’s it ;)

This method has the following signature, writeStringToFile(File file, String data). Let’s try the following example.

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

public class WriteToFileExample
{
  public static void main(String[] args)
  {
    try
    {
      File file = new File("sample.txt");
      String data = "Learning Java Programming";
      FileUtils.writeStringToFile(file, data);
    } catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

The example will create sample.txt file an insert some text in it. When this file is not exist it will automatically created. This example can tell you how easy to read from and write to a file using this library.

Beside of read and write utility you can also use this class to calculate directory size including its sub directory, find files in directory, checking file timestamp, copying file, checking if to file have the same content, etc.

Comment Form

top