Maven Dependency
<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.fool.commons-io</groupId> <artifactId>commons-io</artifactId> <version>1</version> <dependencies> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
delete directory recursively
package org.fool.commons.io;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
/**
* delete directory recursively
*/
public class DeleteDirectoryRecursive
{
public static void main(String[] args)
{
File directory = new File("C:/temp1");
try
{
// Deletes a directory recursively.
FileUtils.deleteDirectory(directory);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
copy directory with all its contents to another directory
package org.fool.commons.io;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
/**
* copy directory with all its contents to another directory
*/
public class DirectoryCopy
{
public static void main(String[] args)
{
// An existing directory to copy.
File srcDir = new File("C:/temp");
// The destination directory to copy to. This directory
// doesn't exists and will be created during the copy
// directory process.
File destDir = new File("C:/temp1");
try
{
// Copy source directory into destination directory
// including its child directories and files. When
// the destination directory is not exists it will
// be created. This copy process also preserve the
// date information of the file.
FileUtils.copyDirectory(srcDir, destDir);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
move directory to another directory with its entire contents
package org.fool.commons.io;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
/**
* move directory to another directory with its entire contents
*/
public class DirectoryMove
{
public static void main(String[] args)
{
File srcDir = new File("C:/temp1");
File destDir = new File("C:/temp2");
try
{
// Move the source directory to the destination directory.
// The destination directory must not exists prior to the
// move process.
FileUtils.moveDirectory(srcDir, destDir);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
calculate directory size
package org.fool.commons.io;
import java.io.File;
import org.apache.commons.io.FileUtils;
/**
* calculate directory size
*/
public class DirectorySizeExample
{
public static void main(String[] args)
{
// Using FileUtils.sizeOfDirectory() we can calculate
// the size of a directory including it sub directory
long size = FileUtils.sizeOfDirectory(new File("C:/temp"));
System.out.println("Size: " + size + " bytes");
}
}
get free space of a drive or volume
package org.fool.commons.io;
import java.io.IOException;
import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;
/**
* get free space of a drive or volume
*/
public class DiskFreeSpace
{
public static void main(String[] args)
{
try
{
String path = "C:";
long freeSpaceKB = FileSystemUtils.freeSpaceKb(path);
long freeSpaceMB = freeSpaceKB / FileUtils.ONE_KB;
long freeSpaceGB = freeSpaceKB / FileUtils.ONE_MB;
System.out.println("Size of " + path + " = " + freeSpaceKB + " KB");
System.out.println("Size of " + path + " = " + freeSpaceMB + " MB");
System.out.println("Size of " + path + " = " + freeSpaceGB + " GB");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
create a copy of a file
package org.fool.commons.io;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
/**
* create a copy of a file
*/
public class FileCopyExample
{
public static void main(String[] args)
{
File source = new File("pom.xml");
File target = new File("pom-backup.xml");
File targetDir = new File("C:/temp");
try
{
System.out.println("Copying " + source + " file to " + target);
FileUtils.copyFile(source, target);
System.out.println("Copying " + source + " file to " + targetDir);
FileUtils.copyFileToDirectory(source, targetDir);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
sort files base on their last modified date
package org.fool.commons.io;
import java.io.File;
import java.util.Arrays;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
/**
* sort files base on their last modified date
*/
public class FileSortByLastModified
{
public static void main(String[] args)
{
File dir = new File("C:/temp");
File[] files = dir.listFiles();
// Sort files in ascending order base on last modification date.
Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
for (File file : files)
{
System.out.printf("File %s - %2$tm %2$te,%2$tY%n", file.getName(),
file.lastModified());
}
System.out.println();
// Sort files in descending order base on last modification date.
Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
for (File file : files)
{
System.out.printf("File %s - %2$tm %2$te,%2$tY%n", file.getName(),
file.lastModified());
}
}
}
read a file into byte array
package org.fool.commons.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
/**
* read a file into byte array
*/
public class FileToByteArray
{
public static void main(String[] args)
{
File file = new File("sample.txt");
try
{
InputStream is = new FileInputStream(file);
byte[] bytes = IOUtils.toByteArray(is);
System.out.println("Byte array size: " + bytes.length);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
get the content of an InputStream as a String
package org.fool.commons.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
/**
* get the content of an InputStream as a String
*/
public class InputStreamToString
{
public static void main(String[] args)
{
InputStream is = null;
try
{
is = new FileInputStream(new File("sample.txt"));
String contents = IOUtils.toString(is, "UTF-8");
System.out.println(contents);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
IOUtils.closeQuietly(is);
}
}
}
create a human-readable file size
package org.fool.commons.io;
import java.io.File;
import org.apache.commons.io.FileUtils;
/**
* create a human-readable file size
*/
public class ReadableFileSize
{
public static void main(String[] args)
{
File file = new File(
"C:/TDDOWNLOAD/JavaTools/JDK/jdk-6u38-windows-x64.exe");
long size = file.length();
String display = FileUtils.byteCountToDisplaySize(size);
System.out.println("Name = " + file.getName());
System.out.println("size = " + size);
System.out.println("Display = " + display);
}
}
read content line by line
package org.fool.commons.io;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
/**
* read content line by line
*/
public class ReadFileToListSample
{
public static void main(String[] args)
{
File file = new File("sample.txt");
List<String> contents = null;
try
{
contents = FileUtils.readLines(file);
}
catch (IOException e)
{
e.printStackTrace();
}
for (String line : contents)
{
System.out.println(line);
}
}
}
write string data to file
package org.fool.commons.io;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
/**
* write string data to file
*/
public class WriteStringToFileExample
{
public static void main(String[] args)
{
File file = new File("sample.txt");
String data = "Apache Commons IO";
try
{
FileUtils.writeStringToFile(file, data);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
read file contents to string
package org.fool.commons.io;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
/**
* read file contents to string
*/
public class ReadFileToStringExample
{
public static void main(String[] args)
{
File file = new File("sample.txt");
String data = null;
try
{
data = FileUtils.readFileToString(file);
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println(data);
}
}
search for files recursively
package org.fool.commons.io;
import java.io.File;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
/**
* search for files recursively
*/
public class SearchFileRecursive
{
public static void main(String[] args)
{
File root = new File("C:/temp");
String[] extensions = { "xml", "java", "dat" };
boolean recursive = true;
Collection<File> files = FileUtils.listFiles(root, extensions,
recursive);
for (File file : files)
{
System.out.println("File = " + file.getAbsolutePath());
}
}
}
touch a file
package org.fool.commons.io;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
/**
* touch a file
*/
public class TouchFileExample
{
public static void main(String[] args)
{
File file = new File("touch.dat");
try
{
// Touch the file, when the file is not exist a new file will be
// created. If the file exist change the file timestamp.
FileUtils.touch(file);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}