GeeCON Prague 2014*
The moose likes Servlets and the fly likes How to read data from a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How to read data from a text file" Watch "How to read data from a text file" New topic
Author

How to read data from a text file

Eric Wang
Greenhorn

Joined: Dec 21, 2000
Posts: 22
HI, All:
Can anyone give me some source code, how do I read some text from a text file, and then print it out. I am trying the source code below, but it always said the file not found. What is wrong with that?
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class readtext extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException{
PrintWriter out=res.getWriter();
res.setContentType("text/html"); // Required for HTTP
out.println(" The begin :");
File f = new File("test.txt");
FileInputStream fin = null;
BufferedReader reader = null;
if (f.exists()) {
fin = new FileInputStream(f);
reader = new BufferedReader(new InputStreamReader(fin));
String line;
while (reader.ready()) {
line = reader.readLine();
out.println(line);
line = reader.readLine();
}
} else out.println("no such file! ");
out.println("The end!");
}
}

ali farid
Ranch Hand

Joined: Aug 26, 2000
Posts: 60
The code should work fine provided the "test.txt" should exist in the same directory.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Eric,
Whenever we use file names, always try to use in this way.

regds
maha anna

[This message has been edited by maha anna (edited December 22, 2000).]
Satish Kasala
Greenhorn

Joined: Dec 19, 2000
Posts: 16
hi maha anna,
I have a question on getServletContext(). Does all the servlet engines implement the above method the same way or is there any variations in the implementation. If I am not wrong getServletContext() should be one of the implementation of the servlet engine.
Satish
Eric Wang
Greenhorn

Joined: Dec 21, 2000
Posts: 22
Hi, maha anna:
I am using this code below, when I try to complie it, they give me this error message.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class readtext extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException{
PrintWriter out=res.getWriter();
res.setContentType("text/html"); // Required for HTTP
out.println(" The begin :");

HttpServletContext con = getServletContext();
String path = con.getRealPath("e:\text.txt");
File f = new File("e:\tex1t.txt");
//below is the same.

Error message:
symbol : class HttpServletContext
locationg: class readtext
HttpServletContext con=getServletContext();
^
1 error

what is wrong with this, or you have some other example, can read data from a text file.
Thanks for the help
Hongwei
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Hongwei,
The reason for the compile error in your is , it is 'ServletContext' not 'HttpServletContext'. Sorry about that.
Whenever we try to refer to a file in a web application , try to refer with respect to virtual path. Because we are talking in terms of OUR WEB APPLICATION. If we adhere to Servlet 2.2 spec, a web appln has a standard dir structure. Which says what dirs contain what types of files.
All servlet's .class files and other supporting .class files go into /WEB-APP-ROOT/web-inf/classes/ dir
all .html and .jsp , and other txt files go into /WEB-APP/ dir . etc.
So we should not try to hard-code any physical dir path in our source code. As in your code c:/ or e:/ etc. Then our application will not be portable in another servlet container/server.
I wrote a small example and tested. Please go through.
Eric Wang
Greenhorn

Joined: Dec 21, 2000
Posts: 22
Hi Maha anna;
Thank you so much, your code works perfect, I am so happy, but one thing I wang to ask you is:
In my text file, it is line by line, but after I read by the servlet, it become one line only. How to slove this problem?
Secondly: what is the command for start another line, just like we press the "Enter" key.
Thank you so much for your help.
Eric Wang
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Eric,
Try one of these tricks.

OR

regds
maha anna
[This message has been edited by maha anna (edited December 30, 2000).]
Eric Wang
Greenhorn

Joined: Dec 21, 2000
Posts: 22
Hi, Maha:
Happy new year to you first. The code you give me works very well, now it line by line. Thank you very much.
Just another want to ask you is: what is the command for start a new line.
I know is ASP is chr(13) for "<Enter>". chr(65) for "A" ...
So what is the same code in JSP or Javaservlet? I hope you understand what my question is.
Cheers
Eric
 
GeeCON Prague 2014
 
subject: How to read data from a text file