How do I copy directory with all its contents to another directory?

To copy a directory with the entire child directories and files we can use a handy method provided by the commons-io FileUtils.copyDirectory(). This method accept two parameters, the source directory and the destination directory. The source directory should be exist while if the destination directory doesn’t exists it will be created.

package org.kodejava.example.commons.io;

import org.apache.commons.io.FileUtils;

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

public class DirectoryCopy {
    public static void main(String[] args) {
        //
        // An existing directory to copy.
        //
        String source = "C:/Demo/source";
        File srcDir = new File(source);

        //
        // The destination directory to copy to. This directory
        // doesn't exists and will be created during the copy
        // directory process.
        //
        String destination = "C:/Demo/target";
        File destDir = new File(destination);

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

How do I copy a URL into a file?

The code snippet below show you how to use the FileUtils.copyURLToFile(URL, File) method of the Apache Commons IO library to help you to copy the contents of a URL directly into a file.

package org.kodejava.example.commons.io;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class URLToFile {
    public static void main(String[] args) {
        try {
            URL url = new URL("//www.google.com");
            File destination = new File("google.html");

            // Copy bytes from the URL to the destination file.
            FileUtils.copyURLToFile(url, destination);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Maven Dependencies

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

How do I get free space of a drive or volume?

The getFreeSpaceKb(String path) method call of the FileSystemUtils class can help you to calculate the free space of a drive or volume in kilo bytes.

Beside using commons io solution, you can use the File.getFreeSpace() method call provided in the Java 1.6 API. You can find an example of it in the following link: How do I get total space and free space of my disk?.

package org.kodejava.example.commons.io;

import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;

import java.io.IOException;

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

An example result of the code above is:

Free space of C: = 121961228 KB
Free space of C: = 119102 MB
Free space of C: = 116 GB

How do I read a file into byte array using Commons IO?

The following example shows you how to read file contents into byte array. We use the IOUtils class of the commons-io library.

package org.kodejava.example.commons.io;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileToByteArray {
    public static void main(String[] args) {
        File file = new File("src/main/resources/Hello.txt");
        try {
            InputStream is = new FileInputStream(file);
            byte[] bytes = IOUtils.toByteArray(is);

            System.out.println("Byte array size: " + bytes.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I get the content of an InputStream as a String?

We can use the code below to convert the content of an InputStream into a String. At first we use FileInputStream create to a connection to a file that going to be read. IOUtils.toString(InputStream input, String encoding) method gets the content of the InputStream and returns a string representation of it. In the end we need to close the InputStream in a finally block.

package org.kodejava.example.commons.io;

import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.File;

public class InputStreamToString {
    public static void main(String[] args) throws Exception {
        //
        // Create an input stream for reading data.txt file content.
        //
        InputStream is = new FileInputStream(new File("data.txt"));

        //
        // Get the content of an input stream as a string using UTF-8
        // as the character encoding.
        //
        try {
            String contents = IOUtils.toString(is, "UTF-8");
            System.out.println(contents);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}