How do I open a file using the default registered application?

If you want to open a file using the default registered or associated application for those files you can use the Desktop.open(File file) method call. In the example below we’ll ask the Desktop class to open a text file.

package org.kodejava.example.awt;

import java.awt.*;
import java.io.File;
import java.io.IOException;

public class RunningDefaultAppOpen {
    public static void main(String[] args) {
        // A reference to a text file
        File file = new File("data.txt");

        try {
            Desktop desktop = Desktop.getDesktop();

            // Open a file using the default program for the file type. In the example 
            // we will launch a default registered program to open a text file. For 
            // example on Windows operating system this call might launch a notepad.exe 
            // to open the file.
            desktop.open(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I copy file using FileChannel class?

The example below show you how to copy a file using the java.nio.channels.FileChannel class.

package org.kodejava.example.io;

import java.io.*;
import java.nio.channels.FileChannel;

public class FileCopy {
    public static void main(String[] args) {
        //// Define the source and target file
        File source = new File("D:/Temp/source.txt");
        File target = new File("D:/Temp/target.txt");

        // Create the source and target channel
        try (FileChannel sourceChannel = new FileInputStream(source).getChannel();
             FileChannel targetChannel = new FileOutputStream(target).getChannel()) {

            // Copy data from the source channel into the target channel
            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

How do I read a file into byte array?

package org.kodejava.example.io;

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

public class ReadFileIntoByteArray {
    public static void main(String[] args) throws IOException {
        File file = new File("Hello.txt");
        try (InputStream is = new FileInputStream(file)) {
            if (file.length() > Integer.MAX_VALUE) {
                throw new IOException("File is too large.");
            }

            int offset = 0;
            int bytesRead;
            byte[] bytes = new byte[(int) file.length()];
            while (offset < bytes.length
                && (bytesRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += bytesRead;
            }
        }
    }
}

How do I create a web based file upload?

This example using the Apache Commons FileUpload library to create a simple application for uploading files. The program is divided into two parts, a form using JSP and a servlet for handling the upload process. To run the sample you need to download the Commons FileUpload and Commons IO get the latest stable version.

File Upload Form

The first step is to create the upload form. The form contains two fields for selecting file to be uploaded and a submit button. The form should have an enctype attribute and the value is multipart/form-data. We use a post method and the submit process is handled by the FileUploadDemoServlet as defined in the action attribute.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>File Upload</title>
</head>

<body>
    <h1>File Upload Form</h1>
    <hr/>

    <fieldset>
        <legend>Upload File</legend>
        <form action="/uploadservlet" method="post" enctype="multipart/form-data">
            <label for="filename_1">File: </label>
            <input id="filename_1" type="file" name="filename_1" size="50"/><br/>
            <label for="filename_2">File: </label>
            <input id="filename_2" type="file" name="filename_2" size="50"/><br/>
            <br/>
            <input type="submit" value="Upload File"/>
        </form>
    </fieldset>
</body>
</html>

The second step is to create the servlet. The doPost method checks to see if the request contains a multipart content. After that we create a FileItemFactory, in this example we use the DiskFileItemFactory which is the default factory for FileItem. This factory creates an instance of FileItem and stored it either in memory or in a temporary file on disk depending on its content size.

The ServletFileUpload handles multiple files upload that we’ve specified in the form above sent using the multipart/mixed encoding type. The process of storing the data is determined by the FileItemFactory passed to the ServletFileUpload class.

The next steps is to parse the multipart/form-data stream by calling the ServletFileUpload.parseRequest(HttpServletRequest request) method. The parse process return a list of FileItem. After that we iterate on the list and check to see if FileItem representing an uploaded file or a a simple form field. If it is represent an uploaded file we write the FileItem content to a file.

So here is the FileUploadDemoServlet.

package org.kodejava.example.commons.fileupload;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class FileUploadDemoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);

        if (isMultipart) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);

            try {
                List items = upload.parseRequest(request);
                Iterator iterator = items.iterator();
                while (iterator.hasNext()) {
                    FileItem item = (FileItem) iterator.next();

                    if (!item.isFormField()) {
                        String fileName = item.getName();

                        String root = getServletContext().getRealPath("/");
                        File path = new File(root + "/uploads");
                        if (!path.exists()) {
                            boolean status = path.mkdirs();
                        }

                        File uploadedFile = new File(path + "/" + fileName);
                        System.out.println(uploadedFile.getAbsolutePath());
                        item.write(uploadedFile);
                    }
                }
            } catch (FileUploadException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Finally we need to register the servlet and create a servlet mapping in the application web.xml file. Below is the content of web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">

    <servlet>
        <servlet-name>FileUploadDemoServlet</servlet-name>
        <servlet-class>org.kodejava.example.commons.fileupload.FileUploadDemoServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FileUploadDemoServlet</servlet-name>
        <url-pattern>/uploadservlet</url-pattern>
    </servlet-mapping>
</web-app>

Maven Dependencies

<!-- https://search.maven.org/remotecontent?filepath=commons-fileupload/commons-fileupload/1.4/commons-fileupload-1.4.jar -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.4</version>
</dependency>

Maven Central

How do I create temporary file?

A temporary file can be created by using java.io.File.createTempFile() method. It accepts the prefix, suffix and the path where the file will be stored. When no path is specified it will use the platform default temporary folder.

The name of the temporary file will be in the form of prefix plus five or more random characters plus the suffix. When the suffix is null a default .tmp will be used for suffix.

package org.kodejava.example.io;

import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;

public class TemporaryFileDemo {
    public static void main(String[] args) throws Exception {
        // Create a temporary file userlist.txt in the default platform
        // temporary folder / directory. We can get the platform temporary
        // folder using System.getProperty("java.io.tmpdir")
        File user = File.createTempFile("userlist", ".txt");

        // Delete the file when the virtual machine is terminated.
        user.deleteOnExit();

        // Create a temporary file data.txt in the user specified folder.
        File data = File.createTempFile("data", ".txt", new File("C:/Users/kodejava/Data"));
        data.deleteOnExit();

        // Write data into temporary file
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(data))) {
            writer.write("750101,2008-01-01,BLUE,CAR");
            writer.write("750102,2008-09-06,RED,CAR");
            writer.write("750103,2008-05-01,GREEN,CAR");
            writer.write("750104,2008-06-08,YELLOW,CAR");
        }
    }
}

How do I display file contents in hexadecimal?

In this program we read the file contents byte by byte and then print the value in hexadecimal format. As an alternative to read a single byte we can read the file contents into array of bytes at once to process the file faster.

package org.kodejava.example.io;

import java.io.FileInputStream;

public class HexDumpDemo {
    public static void main(String[] args) throws Exception {
        // Open the file using FileInputStream
        try (FileInputStream fis = new FileInputStream("C:/Users/kodejava/data.txt")) {
            // A variable to hold a single byte of the file data
            int i = 0;

            // A counter to print a new line every 16 bytes read.
            int count = 0;

            // Read till the end of the file and print the byte in hexadecimal
            // valueS.
            while ((i = fis.read()) != -1) {
                System.out.printf("%02X ", i);
                count++;

                if (count == 16) {
                    System.out.println("");
                    count = 0;
                }
            }
        }
    }
}

And here are some result from the file read by the above program.

31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 
37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 
33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 
39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 
35 36 37 38 39 30 0A 

How do I sort files based on their last modified date?

This example demonstrates how to use Apache Commons IO LastModifiedFileComparator class to sort files based on their last modified date in ascending and descending order. There are two comparators defined in this class, the LASTMODIFIED_COMPARATOR and the LASTMODIFIED_REVERSE.

package org.kodejava.example.commons.io;

import static org.apache.commons.io.comparator.LastModifiedFileComparator.*;

import java.io.File;
import java.util.Arrays;

public class FileSortLastModified {
    public static void main(String[] args) {
        File dir = new File(System.getProperty("user.home"));
        File[] files = dir.listFiles();

        if (files != null) {
            // Sort files in ascending order based on file's last
            // modification date.
            System.out.println("Ascending order.");
            Arrays.sort(files, LASTMODIFIED_COMPARATOR);
            FileSortLastModified.displayFileOrder(files);

            System.out.println("------------------------------------");

            // Sort files in descending order based on file's last
            // modification date.
            System.out.println("Descending order.");
            Arrays.sort(files, LASTMODIFIED_REVERSE);
            FileSortLastModified.displayFileOrder(files);
        }
    }

    private static void displayFileOrder(File[] files) {
        for (File file : files) {
            System.out.printf("%2$td/%2$tm/%2$tY - %s%n", file.getName(),
                file.lastModified());
        }
    }
}

Here are the example results produced by the code snippet:

Ascending order.
15/12/2016 - ntuser.dat.LOG1
15/12/2016 - ntuser.ini
15/12/2016 - .m2
18/12/2016 - Contacts
22/12/2016 - Videos
01/01/2017 - VirtualBox VMs
02/01/2017 - Desktop
02/01/2017 - Documents
------------------------------------------
Descending order.
02/01/2017 - Documents
02/01/2017 - Desktop
01/01/2017 - VirtualBox VMs
22/12/2016 - Videos
18/12/2016 - Contacts
15/12/2016 - .m2
15/12/2016 - ntuser.ini
15/12/2016 - ntuser.dat.LOG1

Maven Dependencies

<!-- http://repo1.maven.org/maven2/commons-io/commons-io/2.6/commons-io-2.6.jar -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

How do I get the list of file system root?

The code below demonstrates how to obtain file system root available on your system. In Linux you will have a single root (/) while on Windows you could get C:\ or D:\ that represent the root drives.

package org.kodejava.example.io;

import java.io.File;

public class FileSystemRoot {
    public static void main(String[] args) {
        // List the available filesystem roots.
        File[] root = File.listRoots();

        // Iterate the entire filesystem roots.
        for (File file : root) {
            System.out.println("Root: " + file.getAbsolutePath());
        }
    }
}

The result of the code snippet:

Root: C:\
Root: D:\

How do I check if a directory is not empty?

package org.kodejava.example.io;

import java.io.File;

public class EmptyDirCheck {
    public static void main(String[] args) {
        File file = new File("C:/Users/kodejava/Documents");

        // Check to see if the object represent a directory.
        if (file.isDirectory()) {
            // Get list of file in the directory. When its length is not zero
            // the folder is not empty.
            String[] files = file.list();

            if (files != null && files.length > 0) {
                System.out.println("The " + file.getPath() + " is not empty!");
            }
        }
    }
}

How do I create a human-readable file size?

This example demonstrate how to create a human-readable file size using the FileUtils class of the Apache Commons IO library. The byteCountToDisplaySize() method take the file size in bytes and return a human-readable display of the file size in bytes, kilobytes, megabytes, gigabytes, etc.

package org.kodejava.example.commons.io;

import org.apache.commons.io.FileUtils;

import java.io.File;

public class ReadableFileSize {
    public static void main(String[] args) {
        File file = new File("/Users/kodejava/Downloads/Java/JDK/jdk-6u7-linux-i586.bin");

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

Here are the result of our program:

Name    = jdk-6u7-linux-i586.bin
Size    = 78482905
Display = 74 MB

Maven Dependencies

<!-- http://repo1.maven.org/maven2/commons-io/commons-io/2.6/commons-io-2.6.jar -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>