I have a string using which I need to create a file object and I don't want to store to the hard-disk.
Is there anyway to create File object from reading the contents from buffer.
Say, for example: I have the string: String s="hello, this is the contents of the file". I need to create the File object for this contents and I don't want to store/retrive to/from hard-disk.
Joined: Jun 19, 2001
i dont think that there is way to do this since when you read from a File, you do not read via the File object but through a FileInputStream which is constructed with the File or the filepath (the File classs has no read/write methods). So you should work with InputStream which can be either a FileInputStream (from filesystem) or a ByteArrayInputSteam or a XYZInputStream.
Joined: Oct 12, 2000
File is just a handle to a physical file or directory on disk, it's not equipped to handle memory-mapped files.
On linux you may have virtual-filesystem support. And on dos, you could create a ramdisk too in former times - don't know about today. Of course this would make installation of your program on different machines difficult...
I liked Pascal's mention of the streams involved. I'm guessing from your requirement for a File object you are calling a method that expects a file:
Could you change the method to want an inputstream argument?
This kind of thinking comes up all the time in "test first development" where you write JUnit tests before you write the code. It's dead simple for a unittest to create an input stream froma string, just as you wanted to do, but a real hassle to create a disk file, run the test and erase the disk file.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi