Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading lines from a file

Status
Not open for further replies.

bos

Technical User
Oct 1, 2002
50
US
I want to be able to read a file line by line so that I can use the information to create checkboxes. Could someone give me a simple example of how to read a file line by line?

Thanks
 
Here is servlet to read data from a text file.

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class ChurchDataServlet extends HttpServlet{
private List columns = new ArrayList();
public void put(String s) {
columns.add((Object) s);}


public String get(int i){
return (String) columns.get(i);}

public void doGet(HttpServletRequest

request,HttpServletResponse response)
throws IOException, ServletException{

PrintWriter out = response.getWriter();
response.setContentType("text/HTML");
int k = 0;
ChurchDataServlet table = null;

try{

table = new ChurchDataServlet();


out.println("Episcopal Church Data for Selected Years");
String s;

BufferedReader r = new BufferedReader(new FileReader (a:\\ChurchData.txt"));

while((s=r.readLine())!= null)
table.put(s);
}

catch(IOException e) {

out.println("oop");
}
k=table.columns.size();

response.setContentType("text/html");

out.println(&quot;<table cellpadding = 9 border=5>&quot;);

for(int i=0;i<k; i++) {

StringTokenizer st = new StringTokenizer((String) table.get(i));
ChurchDataServlet rows = null;
rows = new ChurchDataServlet();

while(st.hasMoreTokens())
rows.put(st.nextToken());
out.println(&quot;<tr>&quot;);
ListIterator l = rows.columns.listIterator();
while(l.hasNext()){


out.println(&quot;<td align = center bgcolor=CCFFFF>&quot;);

out.println(&quot; &quot; + l.next() + &quot; &quot;);
out.println(&quot;</td>&quot;);}

out.println(&quot;</tr>&quot;);
st=null;
}

out.println(&quot;</table>&quot;);

}

}



I copied this from TextPad and it scrambled it. I tried to correct it for you. Hope it helps. You would need a text file named ChurchData.txt on a floppy. Space delimited in the text file.

String Tokenizer is the secret.

hobh




 
Thanks, that was just what I needed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top