Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Files and Streams

Posted on 2004-08-09
5
Medium Priority
?
392 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 400 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

824 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