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 append data to a text file?

One of the common task related to a text file is to append or add some contents to the file. It’s really simple to do this in Java using a FileWriter class. This class has a constructor that accept a boolean parameter call append. By setting this value to true a new data will be appended at the end of the file when we write a new data to it.

Let’s see an example.

package org.kodejava.example.io;

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

public class AppendFileExample {
    public static void main(String[] args) {
        File file = new File("user.txt");

        try (FileWriter writer = new FileWriter(file, true)) {
            writer.write("username=kodejava;password=secret" + System.lineSeparator());
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I create and write data into text file?

Here is a code example for creating text file and put some texts in it. This program will create a file called write.txt. To create and write a text file we do the following steps:

File file = new File("write.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

Below is the complete code snippet:

package org.kodejava.example.io;

import java.io.*;

public class WriteTextFileExample {
    public static void main(String[] args) {
        File file = new File("write.txt");

        try (Writer writer = new BufferedWriter(new FileWriter(file))) {
            String contents = "The quick brown fox" + 
                System.getProperty("line.separator") + "jumps over the lazy dog.";

            writer.write(contents);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

To read a text file see the following example: How do I read a text file using BufferedReader?.