How to Create Temp File in Java


This example will explain how to create a temporary file in Java.

Source Code

package com.beginner.examples;

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

public class CreateTemporaryFile {

	public static void main(String[] args) {
		try {

			File temp = File.createTempFile("pattern", ".suffix");

			temp.deleteOnExit();

			BufferedWriter out = new BufferedWriter(new FileWriter(temp));
			out.write("aString");
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments