• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creation of File object on the fly....  RSS feed

 
Gurumurthy Ramamurthy
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

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.

Thanks,
Guru
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

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.


cheers

pascal
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File is just a handle to a physical file or directory on disk, it's not equipped to handle memory-mapped files.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 unit test 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!