How to create a temporary file?
SolutionThis example shows how to create a temporary file using createTempFile() method of File class.
import java.io.*; public class Main { public static void main(String[] args) throws Exception { File temp = File.createTempFile ("pattern", ".suffix"); temp.deleteOnExit(); BufferedWriter out = new BufferedWriter (new FileWriter(temp)); out.write("aString"); System.out.println("temporary file created:"); out.close(); } }Result
The above code sample will produce the following result.
temporary file created:
The following is another sample example of create a temporary file in java
import java.io.File; import java.io.IOException; public class CreateTempFileExample { public static void main(String[] args) { try { File f1 = File.createTempFile("temp-file-name", ".tmp"); System.out.println("Temp file : " + f1.getAbsolutePath()); } catch(IOException e) { e.printStackTrace(); } } }
The above code sample will produce the following result.
Temp file : C:\Users\TUTORI~1\AppData\Local\Temp\temp-file-name3671918809488333932.tmp
java_files.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4