• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to read data from a text file

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!");
}
}

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code should work fine provided the "test.txt" should exist in the same directory.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic