Apache Commons IO - FileUtils Class



Overview

FileUtils class provides method to manipulates files like moving, opening, checking existence, reading of file etc. These methods use File Object.

Class Declaration

Following is the declaration for org.apache.commons.io.FileUtils Class −

public class FileUtils
   extends Object

Features of FileUtils

The features of FileUtils are stated below −

  • Methods to write to a file.
  • Methods to read from a file.
  • Methods to make a directory including parent directories.
  • Methods to copy files and directories.
  • Methods to delete files and directories.
  • Methods to convert to and from a URL.
  • Methods to list files and directories by filter and extension.
  • Methods to compare file content.
  • Methods to file last changed date.
  • Methods to calculating a checksum.

Here is the input file we're using in our examples −

input.txt

Welcome to TutorialsPoint. 
Simply Easy Learning.

Example - Writing to a Temporary directory using FileUtils

CommonsIoTester.java

package com.tutorialspoint;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class CommonsIoTester {
   public static void main(String[] args) throws IOException {
      //get the file object
      File file = FileUtils.getFile("input.txt");

      //get the temp directory
      File tmpDir = FileUtils.getTempDirectory();

      System.out.println(tmpDir.getName());

      //copy file to temp directory
      FileUtils.copyFileToDirectory(file, tmpDir);
	  
	  System.out.println("File written successfully.");
   }
}

Output

It will print the following result −

Temp
File written successfully.

Example - Reading from Temporary directory using FileUtils

CommonsIoTester.java

package com.tutorialspoint;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.commons.io.FileUtils;

public class CommonsIoTester {
   public static void main(String[] args) throws IOException {
      //get the temp directory
      File tmpDir = FileUtils.getTempDirectory();
	      
      //create a new file
      File newTempFile = FileUtils.getFile(tmpDir, "input.txt");

      //get the content
      String data = FileUtils.readFileToString(newTempFile, Charset.defaultCharset());

      //print the content
      System.out.println(data);
   }
}

Output

It will print the following result −

Welcome to TutorialsPoint. 
Simply Easy Learning.

Example - Copy File using FileUtils

CommonsIoTester.java

package com.tutorialspoint;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class CommonsIoTester {
   public static void main(String[] args) throws IOException {
      //get the file objects
      File sourceFile = new File("input.txt");
	  File destinationFile = new File("output.txt");
	  
	  // copy file
      FileUtils.copyFile(sourceFile, destinationFile);
      System.out.println("input.txt copied to output.txt");
   }
}

Output

It will print the following result −

input.txt copied to output.txt

You can check the content of output.txt file created in current directory as −

Welcome to TutorialsPoint. 
Simply Easy Learning.
Advertisements