How to create a file in Windows that would have attributes FILE_ATTRIBUTE_TEMPORARY
and FILE_FLAG_DELETE_ON_CLOSE
set using Java?
I do want my file to be just in-memory file.
To precise: delete-on-exit mechanism does not satisfy me, because I want to avoid situation, when some data is left on disk in case of, for example, application crash.
The File class in Java provides a method with name createTempFile(). This method accepts two String variables representing the prefix (starting name) and suffix(extension) of the temp file and a File object representing the directory (abstract path) at which you need to create the file.
io. tmpdir is a standard Java system property which is used by the disk-based storage policies. It determines where the JVM writes temporary files, including those written by these storage policies (see Section 4 and Appendix A. 8). The default value is typically " /tmp " on Unix-like platforms.
What are temporary files? Temporary files, also called temp or tmp files, are created by Windows or programs on your computer to hold data while a permanent file is being written or updated. The data will be transferred to a permanent file when the task is complete, or when the program is closed.
The default value is typically "/tmp" , or "/var/tmp" on Unix-like platforms. On Microsoft Windows systems the java. io. tmpdir property is typically "C:\\WINNT\\TEMP" .
Use something like this. It won't be in-memory though, but a temporary file that is deleted when the app exits.
try {
// Create temp file.
File temp = File.createTempFile("pattern", ".suffix");
// Delete temp file when program exits.
temp.deleteOnExit();
// Write to temp file
BufferedWriter out = new BufferedWriter(new FileWriter(temp));
out.write("aString");
out.close();
} catch (IOException e) {
// (..)
}
Why not just use a memory block i.e. datastructure ? What's the incentive behind creating a file ? If you want a scratch file then temp file and delete on exit will help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With