Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read file from current dir in webserver

 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I am using tomcat webserver and I am having difficulty reading files. It seems that by default it read from C drive everytime i execute a servlet. How do i make it read file from current dir the servlet execute? For example WEB-INF\classes\myclass
Thanks
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No by default it reads from /WEB-INF/classes folder. please send your code to better understand the problem.
 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is part of the code in web-inf/classes/PM. If take away the file from c drive it doesn't work but when put it back it works. What are the explanation for this? Thanks
package PM;
import java.net.*;
import java.io.*;
public class week{

try{
FileInputStream istream = new FileInputStream("do.txt");
 
Kunaal A Trehan
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Why donot u use servelet context.
Then u can read the data as input stream
Eg.
ServLetContext contx=Session.getServletContext();
InputStream inptStr = contx.getResourceAsStream("WEB-INF/IO/FILENAME")
For this u need to put the file in some directory under WEB-INF folder
I hope this helps
Rgds
Kunal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic