Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Create a "virtual file"  RSS feed

 
Swerrgy Smith
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I am using an API like that : doSomething(File file);
I want to create a File object (that has content) then call the function doSomething(file). However, I do not have the right to write this file to the disk.
For example, it is not possible to do like that:
f=new File("myfile.txt");
if(!f.exists()) f.createNewFile()
Could anyone tell me how to create a File object that has content without writing it to disk?

Thank you.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a particular reason that API method needs a File to work with? Since it's sometimes inconvenient to work with files (as you have found out), there's often an alternative method that takes an InputStream instead. Or if there isn't, maybe one could be provided by whoever maintains the API?
 
Swerrgy Smith
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Is there a particular reason that API method needs a File to work with? Since it's sometimes inconvenient to work with files (as you have found out), there's often an alternative method that takes an InputStream instead. Or if there isn't, maybe one could be provided by whoever maintains the API?


Well, this is the API of iText 1.4.8
com.lowagie.tools.Executable.printDocumentSilent(File file);
OR
com.lowagie.tools.Executable.printDocumentSilent(String fileName);

In the first case we need a File object, in the second case we need a file which physically exists and has a name.

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone tell me how to create a File object that has content

If you're talking about a java.io.File, revisit the first sentence of API for the class.
An abstract representation of file and directory pathnames.


There's no way a File object can have 'content'
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just found what looks like the source code for that class:

http://www.docjar.com/html/api/com/lowagie/tools/Executable.java.html

Unfortunately, it looks like you're out of luck with using an in-memory stream, as the actual action is delegated to a Runtime.exec() call, to something like (depending on operating system), which means it isn't the Java API doing the actual file manipulation. Unless someone's got a really smart idea, I think you're going to need a real file or a different API.

 
Anil Reddy V
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Try This way




Anil Reddy V
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating a temporary file may just be possible. On both Linux and Windows you should always have a temp folder (/tmp on Linux, a folder inside the user's home folder on Windows) in which you can write files. File.createTempFile uses that folder by default.
 
Swerrgy Smith
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for you suggestions
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!