In Java easiest way to create new file of Zero byte just like unix touch commanad is using touch method of FileUtils class of Apache Commons IO Java library. Commons IO is well tested library. The Commons IO library contains utility classes, stream implementations, file filters, file comparators, endian transformation classes, and much more.
In Unix and Linux touch command is the easiest way to create new, empty files. It is also used to change the timestamps (i.e., dates and times of the most recent access and modification) on existing files and directories.
Platform information
jdk1.8.0_60, Java 8, Eclipse
Library Information
Used library Name: Commons IO
Methods used
//Implements the same behaviour as the "touch" utility on Unix.
//It creates a new file with size 0 or, if the file exists already
//it is opened and closed without modifying it, but updating the file date and time.
public static void touch(File file) throws IOException
Maven Dependency Entry
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
Gradle Dependency Entry
compile \'commons-io:commons-io:2.4\'
Jar Download Location: Download Jar
Sample code to create o size file
package org.code4copy;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class ZeroSizeFile {
public static void main(String[] args) throws IOException {
File file = new File("d:\\tmp.txt");
FileUtils.touch(file);
System.out.println("File length: "+file.length());
}
}
Output of program
File length: 0
Download code from here