Solved

Files and Streams

Posted on 2004-08-09
5
368 Views
Last Modified: 2010-03-31
Question -

A method used AnyMethod(java.io.File , java.io.File ) signature
I have InputStream and string of the data in the file as I  create them on the fly and there is no "file" in the server.
How do I pass the InputStream/string to the method with the above signature.

Context
I am using FOP and it uses xml and fo .
To emberd itin a servelt and convert to PDF it uses

 XSLTInputHandler inputHandler = new XSLTInputHandler(xmlFile, xslFile);

where the xmlFile, xslFile are java.io.File, I have the XML and XSL as InputStreams / strings in my servers memory as they are generated dynamically, how to pass them

Thx
0
Comment
Question by:applekanna
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11750452
Create a file from this input stream:

FileOutputStream fos = new FileOutputStream(new File("myfile.pdf"));
// get the bytes from the input stream
fos.write(<bytes>);
fos.flush();
fos.close();

Then use this file (myfile.pdf) to pass it in the method.
0
 
LVL 3

Author Comment

by:applekanna
ID: 11750518
FileOutputStream fos = new FileOutputStream(new File("myfile.pdf"));

1. when I do something like this , would not I run into concurrency issues in the servlet because of the file name, so to make it use I need to give each file a unique file name (say system time - any other ideas)

2. Also I need to keep deleting the files to take care of memory issues.

I am currently doing the above both

Out of curiosity
Is this the only way ...or is there any other method
0
 
LVL 35

Accepted Solution

by:
girionis earned 100 total points
ID: 11750545
1. It depends if the FileOutputStream is an instance variable or not. If it is you can always synchronize it.

2. Delete them from disk first and then setting them to null would be enough for memory.

I am not sure if there is any other way, it might be though. If the method accepts two parameters of type "File" then you either need to pass a File or a subclass of it. It would be easier if you could change the method's signature and make it an Object so you can either pass a File or an InputStream (and check inside your method for the type of Object passed).
0
 
LVL 3

Author Comment

by:applekanna
ID: 11750613
Thx for ur comments
0
 
LVL 35

Expert Comment

by:girionis
ID: 11750648
Thank you for accepting, glad I was of help :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example 2 31
Is there a simpler dropbox system? 10 44
glassfish admin console 1 16
restrict decimal places for double datatype 10 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question