Windows temporary file in Java
Asked Answered
M

5

5

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.

Manfred answered 25/5, 2010 at 10:31 Comment(0)
M
4

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) { 
// (..)
} 
Metronome answered 25/5, 2010 at 10:34 Comment(3)
Well, I want to pass some data to external Windows (not Java) application using the file. Reading from a file is an only option for that application. And I want to avoid situation, when some data leaves on disk in case, for example, application crash. That's why I think, the temporary Windows file would be the best solution. Delete-on-exit mechanism may leave data on disk in some cases, I think.Bethlehem
@Przemysław, if the app crashes, you don't have control on what can be executed. What about doing a temp file clean up right after the app starts up?Metronome
FILE_ATTRIBUTE_TEMPORARY + FILE_FLAG_DELETE_ON_CLOSE has a chance of leaving a file on disk too. If you use deleteOnExit as well, you reduce the chance because the system memory would have to be under pressure and then the application crash as well. tmpfs only offers a slightly better option, the file would remain available after an application crash until the next reboot.Gosselin
D
2

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.

Dimorphous answered 25/5, 2010 at 10:36 Comment(2)
Well, I want to pass some data to external Windows (not Java) application using the file. Reading from a file is an only option for that application. And I want to avoid situation, when some data leaves on disk in case, for example, application crash. That's why I think, the temporary Windows file would be the best solution. Delete-on-exit mechanism may leave data on disk in some cases, I think.Bethlehem
@PrzemysławRóżycki - the problem is that the contents of files (including temporary ones) are typically still on disc after the file has been deleted! You've heard of "Norton Utilities"?Pluto
B
1

Even with both the flags set your files might end up in the filesystem. If the system cache becomes too small, the file is written to the disk, and if the system crashes, no afterprocess cleanup is performed.

However, I like your idea and wonder why the JVM implementation on Windows doesn't use the flags by default. At least deleteOnExit() should be implemented like this as a fallback.

Brookbrooke answered 25/5, 2010 at 18:56 Comment(0)
P
0

I do want my file to be just in-memory file.

Marking a file as temporary and delete-on-close on Windows won't guarantee that it is not written to the file system.

With UNIX / Linux you could create the file in TmpFS or RamFS file system; i.e. a file system that stores files in RAM memory. TmpFS is backed by virtual memory, so some or all of a file in RamFS may end up on the swap disc. RamFS is not backed by virtual memory, and should only ever reside in RAM.

An overview of RamFS and TmpFS can be found here.

Note however that it is possible (at least in theory) for RamFS contents to end up on disc.

  • If the system is put into hibernate state, the entire contents of RAM is saved to disc before the system is powered down.

  • If the kernel can be induced to crash and kernel crash dumps are enabled, the contents of kernel memory (probably including the RamFS) will be written to the dump.

Pluto answered 25/5, 2010 at 12:1 Comment(10)
It seems to me that both Windows and non-Windows supply a system of temporary files where they are written to disk iff the system is low on memory. This functionality should be accessible to Java programmers via a standard API.Gosselin
You are talking about paging / virtual memory I think. 1) They are not temporary files. 2) Data is not only written to the paging area when space is low. 3) There is no such standard API in Java ... or in any other language ... that allows application programs to treat paging space as a file system. 4) I don't see how such a (hypothetical) API would help the OP.Pluto
No, I am talking about temporary files. By which I mean tmpfs on non-Windows operating systems and FILE_ATTRIBUTE_TEMPORARY+FILE_FLAG_DELETE_ON_CLOSE on Windows. Both allow files to be written into memory and only written to disk when memory pressure dictates. There is no standard API in Java - that is correct but there ARE APIs in C++ for this. It helps the OP because it's EXACTLY what he was asking for. blogs.msdn.com/b/larryosterman/archive/2004/04/19/116084.aspxGosselin
Well in that case, 1) your comment about "a system of temporary files that are written to disk iff system memory is low" is incorrect for non-Windows operating systems. 2) Linux / UNIX systems typically support memory file systems, but they don't have precisely the properties you claim. 3) AFAIK Java provides no I/O APIs that are aware of this special property ... when the OS supports it. 4) This doesn't address the OP's requirements anyway. He wants "files" that are NEVER written to disk.Pluto
He said "How to create a file in Windows that would have attributes FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE set using Java?" I know those APIs don't exist in Java at present, I just think that they should do. I don't think I'm wrong about writing the files to disk iff there's not much memory - it's just that on Windows it's an accessible file and on tmpfs it's in the swap space.Gosselin
@Gosselin - What you think about what the Java APIs ought to do is not particularly relevant. (That is, unless you are secretly a Oracle employee ... and might have some influence.) You are wrong in what you wrote about the semantics because you used the mathematical term "iff". That means "if and only if". Emphasis added. I'd be extremely surprised if the Windows OS implements the "and only if" part. Certainly Linux / UNIX tempfs and memfs file system do no provide those guarantees.Pluto
I stand by my statement of them being written to disk if and only if the system is low on memory. The phrase "is low on memory" might mean different things to different people however - it may just be that the OS decides that the RAM is better used elsewhere. The tmpfs specification says "If a system is low on memory, anonymous pages can be written to a swap device if they are selected by the pageout daemon or are mapped into a process that is being swapped out." The Windows API says similar things.Gosselin
@mjaggard. Saying "if a system is low on memory, anonymous pages can be written to a swap device" ... is NOT the same as saying "pages will be written to a swap device if and only if the system is low on memory". But, if you don't get the logical distinction, there's little point arguing further.Pluto
What possible reason is there for the operating system to write files to disk if there is plenty of free memory? It's not that the specification bans writing pages to disk, it's just obvious that if you have plenty of memory, it won't bother. The "only if" part comes from common sense.Gosselin
@Gosselin - To get the work done ahead of time. Evicting a clean page is faster than evicting a dirty one. So if you can identify a dirty page that is unlikely to be use, and flush it, you can respond faster to a possible demand for more pages. (The paging system used in 4.2bsd used to work like this ... IIRC) But anyway that's not the point. The point is that specifications do not state that it is not done. The "only if" might seem to you to be common sense, but that is not sufficient to assert that it doesn't happen ... especially since there are plausible reasons for doing it.Pluto
B
0

You are after a windows specific solution, so why not create files using wndows commands execed via Processbuilder.

Bastard answered 25/5, 2010 at 13:20 Comment(1)
Whilst this is true from the question, the solution to this could be generic because tmpfs does exactly the same job in Linux.Gosselin

© 2022 - 2024 — McMap. All rights reserved.