Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading a file from bean 1

Status
Not open for further replies.

lowbk

Technical User
Nov 26, 2001
162
SG
normally when we read a file in java, we use
new FileReader(new File(filename));

this is not possible in jsp. i found a method on the internet that uses
InputStream in = getClass().getResourceAsStream(filename);

but then that getclass() cannot be used on static method/class, so if i want to put it in a bean, how should i do it?

or is there another method to read a file via a bean?

thanks
 
You certainly can do this from a jsp. YOu just need to specify the pathing from the context of the jsp so that the container can find the file.

To answer your other question, if your class is named MyClass.java simply do this:

MyClass.getClass().getResourceAsStream(filename);
 
meadandale, thanks for your reply, but i think i need more help. suppose the following:

my jsp is at D:\tomcat321\XMLTransfer\jsp1.jsp

my bean is at (within package thepack) D:\tomcat321\XMLTransfer\WEB-INF\classes\thepack\bean1.java

my context path is <Context path=&quot;&quot; docBase=&quot;XMLTransfer&quot; debug=&quot;0&quot; reloadable=&quot;true&quot;/>

if the file to load is called file1.txt (come to location later).
within bean1.java i'm using this.getClass().getResourceAsStream(filename)

currently within my jsp1.jsp, i'm using bean1.loadfile(&quot;file1.txt&quot;)
and the file is at D:\tomcat321\XMLTransfer\WEB-INF\classes\thepack\file1.txt
u mentioned in the pathing context of the jsp,
but if file1.txt is placed at D:\tomcat321\XMLTransfer\file1.txt , there would be an error


if i use bean1.loadfile(&quot;/file1.txt&quot;), where should the file be placed?
if i want to put file1.txt in D:\tomcat321\XMLTransfer\file1.txt , how should i invoke it?

thanks
 
In your example, the path of your file relative to your webapp is &quot;/file1.txt&quot;. To access this file from within a .jsp you would construct the path as follows:

String fullpath = config.getServletContext().getRealPath(&quot;/file1.txt&quot;);

bean.loadFile(fullpath);

If you want to put the file in another directory below XMLTransfer, just be sure that you reference it relative to the XMLTransfer directory.

Hope this helps,

Charles
 
meadandale, using the above method,
fullpath => &quot;d:\tomcat321\xmltransfer\file1.txt&quot;

the bean cannot load the file, though the file1.txt is in the right place.

any suggestion what could have went wrong?
 
What does the loadfile code in your bean look like?

When I run this in a servlet, the servlet can find and read the file. YOu should be able to pass the path to a bean and have it do the same. I'll write a quick bean to do this in a few hours just to verify.
 
meadandale, very sorry to bother you

below is the code fragment in the jsp (D:\tomcat321\XMLTransfer\jsp1.jsp )
String fullpath = config.getServletContext().getRealPath(&quot;/file1.txt&quot;);
String res = partialdocument.show(fullpath);

below is the code fragment in the bean
(D:\tomcat321\XMLTransfer\WEB-INF\classes\thepack\partialdocumentBean.java )

InputStream in = this.getClass().getResourceAsStream(fullpath);

putting file1.txt in D:\tomcat321\XMLTransfer\file1.txt , in == null but fullpath==D:\tomcat321\XMLTransfer\file1.txt (which is correct)

any suggestion, what went wrong?
 
No,no,no,no...

You are mixing metaphors. Class.getResourceAsStream() requires that the resource be ON THE CLASSPATH. We are trying to avoid that by directly locating the file. What you need to do is something like this in your bean:

InputStream in = new FileInputStream(fullpath);

All the CTOR of the FileInputStream needs to know is the fully qualified path to the file which is what you have created.

Hope this helps.

Charles
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top