• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creation of File object on the fly....

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic