Java Examples to Create New Empty File and A Temporary File

1. Overview


In this tutorial, We'll be learning how to create a file in java and how to create a temp file in java.

Example programs demonstrated in the following methods.

createNewFile()
createTempFile()

Example Programs on Files

createNewFile(): method is used to create a new empty file.
createTempFile(): method is used to create a temporary file.

Java Examples to Create New Empty File and A Temporary File




Program: How to write or store data into a temporary file in java?


2. Creating a new file


File api has a method createNewFile() which is used to create a new empty file. Returns a boolean value, if the file is created any errors then true is returned, else false if the file already exists or file creation is failed.

Syntax:

Below is the syntax for createNewFile() method.

public boolean createNewFile() throws IOException

This will throw IOException.

Example:
package com.java.w3schools.blog.java8.files;

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

/**
*
* Java Examples to Create New Empty File |
*
* @author venkateshn
*
*/
public class FileCreationExample {

public static void main(String[] args) {
File newFile = new File("newfile.txt");
boolean isFileCreated = false;
try {
isFileCreated = newFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

if (isFileCreated) {
System.out.println("New blank file created.");
} else {
System.out.println("File already exists");
}

}

}

Output:

New blank file created.

Once we run the program, It will create a new file. If we execute the same program multiple times then it will return false and prints "File already exists".

3. Creating a temporary file in Java


To create a temporary file, java File api has a method.

createTempFile() method is used to create temp file. For this method, We need to pass the file name and extension. Let us look at the following syntax.

Syntax:

public static File createTempFile​(String prefix, String suffix) throws IOException
public static File createTempFile​(String prefix, String suffix, File directory) throws IOException

This is a static method and an overloaded method. Directly this method can be accessed by class name like File.createTempFile().

Takes two arguments such as prefix and suffix. The prefix indicates the file name and it must be a minimum of three characters. The suffix indicates file type or extension. The suffix can be null, if null is supplied then .tmp will be the extension.

When method executes it appends some random number to the suffix value to make sure the file name is unique even if we rerun the program multiple times.

3.1 Example to create a temp file in the default temporary directory:


By default, createTempFile() method create a temp file in default temp location.

package com.java.w3schools.blog.java8.files;

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

/**
*
* Java Examples to Create a Temporary File
*
* @author venkateshn
*
*/
public class CreateTempFileExample {

public static void main(String[] args) {

try {
File tempFile = File.createTempFile("Test", null);
System.out.println("Test.tmp file created in default location ");
String fileLocation = tempFile.getAbsolutePath();
System.out.println("Defalut temp location: " + fileLocation);
} catch (IOException e) {
e.printStackTrace();
}

}

}

Output:

In the above program, the suffix is passed as null. So, We expect the file must be created with .tmp extension. Let us look at the output. These programs are executed on the mac machine.

Test.tmp file created in default location 
Defalut temp location: /var/folders/g0/0x5m1w1950z18h3m6z6h1_000000gn/T/Test8917927582450411382.tmp


3.2 Example to create CSV file:

try {
File tempFile = File.createTempFile("Employee", ".csv");
System.out.println("Employee.csv file created in default location ");
String fileLocation = tempFile.getAbsolutePath();
System.out.println("Defalut temp location: " + fileLocation);
} catch (IOException e) {
e.printStackTrace();
}


Output:

In the above program suffix is supplied as ".csv" and "." period must be placed before the suffix. If we do not provide. then file name would be like mployee10947352513681704779csv.

Employee.csv file created in default location 
Defalut temp location: /var/folders/g0/0x5m1w1950z18h3m6z6h1_000000gn/T/Employee10947352513681704779.csv

3.3 Creating Temp file in our own directory instead of default dir


We have to use the overloaded createTempFile() method with 3 arguments as below.

public static File createTempFile​(String prefix, String suffix, File directory) throws IOException

The argument directory will take our new directory where the temp file should be created. If we pass it as null then it considers system default temp location.

package com.java.w3schools.blog.java8.files;

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

/**
*
* Java Examples to Create a Temporary File
*
* @author venkateshn
*
*/
public class CreateTempFileExample {

public static void main(String[] args) {

try {
File tempFile = File.createTempFile("hello", ".txt", new File("files"));
System.out.println("hello.txt file created in new location provided");
String fileLocation = tempFile.getAbsolutePath();
System.out.println("Checking the file location created : " + fileLocation);
} catch (IOException e) {
e.printStackTrace();
}
}

}

Output:

hello.txt file created in new location provided
Checking the file location created : /Users/venkateshn/Documents/VenkY/blog/Tutorials/CoreJava/files/hello4719932075815441131.txt

4. Conclusion


In this article, We've covered how to create an empty file using Java File API.

And also seen how to create a temp file in default temporary location and in a custom or given location.

GitHub
File API Ref


0 Comments