I have a text file and a class (Reader) that reads the text file and stores each line in a String [].
String name;
String [] lines;
Reader(String name){
this.name = name;
}
public String toString(){
return this.name;
}
public readFile(String filename){
String line = "";
int i = 0;
try{
BufferedReader reader = new BufferedReader(new FileReader(filename));
while(line = reader.readLine()) != null){
lines[i] = line;
i++;
}// while
reader.close();
}
catch(etc...){}
}
I wish to print each array element in table on my jsp page.
Reader r = new Reader("test");
out.print(r.toString());
works and prints ‘test’ but…
r.readFile("test.txt")
for (int i=0; i < r.lines.length; i++)
out.print(r.lines[i])
does not… However if I run this on the command line its prints the lines [ ] fine
How do I go about doing it in web context?
Try replacing the following:
with
That should work since the input file is in the same package as
Reader.Update:
I think the problem lies in the
TeamData.That’s not a valid path to the file. Neither in filesystem, nor in the classpath.
Since, the data.file is in the classpath, you can use
getResourceAsStreamto load it.And, since skytest is the root directory (package),
"/skytest/data.file"would also be valid here (the leading/means relative package root). Or, since the file lies in the same package as theTeamData, just the file name should be enough"data.file".So, use of the following:
And change the following:
to
Also, the following is really a bad practice (that’s called swallowing the exception):