3

I have an interface that takes java File object, but I have a string I want to send to it. Without writing to disk, how would I create an object suitable to send to that interface?

I have the necessary data/metadata like length, timestamp, content and filename, and I want to include them in the File.

Paul Biggar
  • 27,579
  • 21
  • 99
  • 152
  • How can you send a string to a file without writing it to disk? You mean like store the file in ram so it can be accessed later? – ghostbust555 Aug 09 '13 at 22:03
  • 1
    I know the Java API pretty well, but I think you can't do that. You may create a subclass of File, and implement it as you want (it's not final). But the program would fail when the implementation of your interface tries to make a `FileInputStream` or `FileOutputStream` out of your File. – Giulio Franco Aug 09 '13 at 22:03
  • 1
    This is unfortunately not possible in Java 6. So if you're working with a legacy framework, you're out of luck. You may be able to set up memory mapped files but it would be platform specific. In Java 7 you can write a [custom file system provider](http://download.oracle.com/javase/7/docs/technotes/guides/io/fsp/filesystemprovider.html) which I believe is capable of doing what you want. – aioobe Aug 09 '13 at 22:04
  • see also: http://stackoverflow.com/questions/7083698/create-a-file-object-in-memory-from-a-string-in-java – DannyMo Aug 09 '13 at 22:10
  • @aioobe even if you make a `FileSystemProvider`, you won't be able to make a `File`, but only a `FileStore` or Input/Output streams. You won't be able to have a `File` object that will work like a real File, even though there's not an underlying file. – Giulio Franco Aug 09 '13 at 22:11

1 Answers1

0

So, I don't know if I understood it correctly, but what about using the temp filesystem from Common VFS?

The URI format is tmp://[ absolute-path].

Documentation says:

Provides access to a temporary file system, or scratchpad, that is deleted when Commons VFS shuts down. The temporary file system is backed by a local file system.

dierre
  • 7,140
  • 12
  • 75
  • 120