file reading in java

N

naveen

hi,

i have a problem with file reading in java...
currently i am using the following Environment:
WSAD 5.1.0. for Windows
Oracle 8.1.7. 8i for Windows JDK 1.3.1.08 DB2 Client
IBM DB2 Windows XP
i am getting the "file not found error" in my Console
while retiving the values from the file...

i am calling file reading method from Servlet doPost()

here is the code snippet i used in main:

here is the declaration of the file:
private String _CRIT_Maintain_FileName =
"./WEB_INF/classes/com/d_systemes/osinet/base/Critsit_Maintenance.txt";

in servlet doPost() i am calling the file reader
method..
if
!(UserValidity.isValidUser(UserCurrent,"CRIT",_CRIT_Maintain_FileName))
{....};

the file reading method is:
public static boolean isValidUser(String
currentUser,String flag,String fileName) {


BufferedReader in= null;
try {
//Make sure that the Textfile exists and has valid
data records in a SingleLine.
//Otherwise, exceptions will occur.
in = new BufferedReader(new FileReader(fileName));
String str = null;
String validUser;
int start;
int end;
while ((str = in.readLine()) != null) {
//Using ',' as a Delimeter
if(str.charAt(0)==',')
start=1;
else
start=0;
end=str.indexOf(",");
while
((validUser=str.substring(start,end))!=null)
{
//Checking the cuurentUser value with the text
file value.

if(currentUser.equalsIgnoreCase(validUser.trim()))
{
return(true);
}
start=end+1;
end=str.indexOf(",",end+1);
}
}
in.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}




but at the time of file reading i am getting the file
reading exception..can u plz tell me why i am getting
this error?...i kept the source file and the text file
in the current directory only..
 
W

William Brogden

ere is the declaration of the file:
private String _CRIT_Maintain_FileName =
"./WEB_INF/classes/com/d_systemes/osinet/base/Critsit_Maintenance.txt";

There is your problem - that usage depends on the "current" directory.
In a servlet you have no control over the current directory - look at
the ServletContext API for getRealPath() and getResourceAsStream()

Bill
 
S

shakah

naveen said:
hi,

i have a problem with file reading in java...

[...snipped...]

i am getting the "file not found error" in my Console
while retiving the values from the file...

[...snipped...]

private String _CRIT_Maintain_FileName =
"./WEB_INF/classes/com/d_systemes/osinet/base/Critsit_Maintenance.txt";

[...snipped...]

but at the time of file reading i am getting the file
reading exception..can u plz tell me why i am getting
this error?...i kept the source file and the text file
in the current directory only..

Maybe your path is incorrect relative to the working directory? In
other words, does something like the following work better?
private String _CRIT_Maintain_FileName =
"/usr/local/tomcat/webapps/yourapp/WEB_INF/classes/com/d_systemes/osinet/base/Critsit_Maintenance.txt";

Also, make sure the user under which your servlet engine runs has
permission to read the file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,295
Latest member
EmilG1510

Latest Threads

Top