CharSequence backed by File, auto-delete when JVM exits. Possible?
Hi,
I wanted to write a static method that from any CharSequence constructs a new CharSequence for which the character data is held in a File rather than in memory (I am writing an application that deals with rather huge strings, only portions of which are of interest at a time).
So I did:
File file = File.createTempFile("temp", ".dat");
[write the whole CharSequence to file and close the file, then:]
file.deleteOnExit();
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
and then construct a CharSequence which calls randomAccessFile.seek() whenever charAt() is called (obviously this only works on fixed-length characters).
Unfortunately that way the file doesn't end up getting deleted, probably because the RandomAccessCharSequence is never close()d and therefore the file remains open (deleteOnExit() appears to fail on files that haven't already been closed, at least on win32).
Is there any way I can achieve what I want? I want to return only a CharSequence that can be treated the same way as any other CharSequence by the rest of the program, but I also want to make sure the resources (Files) get deleted when the JVM exits - and sooner than that would be even nicer. Unfortunately Java doesn't have destructors, the finalize() method isn't guaranteed to get called (in my example for some reason apparently it never gets called).
Any ideas? Any help would be appreciated! Thanks.
Tobias

