4

For my usecase, I would like to have an in memory directory to store some files for a very short time. Actually I compile source code to .class files at runtime, classload and execute them. The clean way would be to create a virtual directory and let the compiler create .class files there. Of course I could use a temp directory, but I have to clean it before compiling, I don't know if I'm the only one using it, etc.

So, is and how is it possible to create a virtual, in the meaning of in memory, directory in Java?

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Stefan K.
  • 7,701
  • 6
  • 52
  • 64

5 Answers5

8

In Java 6 it is not really possible to do this kind of thing within a Java application. You need to rely on the OS platform to provide the the pseudo-filesystem.

In Java 7, the NIO APIs have been extended to provide an API that allows you to define new file systems; see FileSystemProvider.

Apache Commons VFS is another option, but it has a couple of characteristics that may cause problems for existing code and (3rd-party) libraries:

  • Files and directories in VFS are named using urls, not File objects. So code that uses File for file manipulation won't work.

  • FileInputStream, FileOutputStream, FileReader and FileWriter won't work with VFS for much the same reason.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

It sounds like you could use a ramdisk. There are many apps out there that will do this, what you use would depend on the target OS. I don't know of any native Java API that supports this.

Unsigned
  • 9,640
  • 4
  • 43
  • 72
1

I am not sure if this is helpful or not, but do check Apache Commons VFS.

It seems that what you need is memory filesystem.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Gilbeg
  • 741
  • 2
  • 9
  • 19
0

For Java7's NIO there are

koppor
  • 19,079
  • 15
  • 119
  • 161
0

I've used jimfs by Google as an in memory file system. It looks like that would be what's needed here.

It's also very nice for unit/integration testing applications that interact with the filesystem.

Malte Esch
  • 1
  • 1
  • 3
  • 1
    If the question could be answered with a link or the name of something, it would be off-topic and should not be answered. If it is not asking for a link then answering it with a link is oonsidered a link-only answer and probably will be flagged as such and deleted. So, please either [edit] to turn this into an answer according to [answer] or delete this. – Yunnosch Aug 20 '23 at 16:12