| Author |
Problem: read text file in java
|
leif stone
Greenhorn
Joined: Nov 02, 2005
Posts: 2
|
|
Hi, guys, I am starter in java and servlet. Please please do help me! that is question: You program must be able to cope with arbitrary number of questions in the file; The servlet must display the following text sequence: ..("There are N questions to answer",where "N" is the number of questions in the file.) ..("Question 1<text for question1> Question2<text for question2>....Question N <text for question N>") (You must preserve the text sequence and spelling. That is, the specified words must appear in the specified order somewhere in your page. Therefore, I need to read list of questions in a external plain text from a text file in java. so, what i gonna do? Thanks a lot!!! Really appreciate if u reply it!
|
 |
Gerardo Tasistro
Ranch Hand
Joined: Feb 08, 2005
Posts: 362
|
|
Ok this is a servlet right? Is the text file uploaded or held as a resource (in your app directories) Can the file be XML???
|
 |
leif stone
Greenhorn
Joined: Nov 02, 2005
Posts: 2
|
|
Thank you for replying fisrt! yea, it's servlet. The file must be text file and put in my app directory.
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Originally posted by leif stone: (You must preserve the text sequence and spelling. That is, the specified words must appear in the specified order somewhere in your page.
Is this a homework assignemnt, Leif?
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Gerardo Tasistro
Ranch Hand
Joined: Feb 08, 2005
Posts: 362
|
|
Ok here is a quick implementation example using Castor (you can use Xstream too). We'll be using XML which is within the scope of your specs. // get a mapping file // that says how your questions are mapped Mapping eventoMapping=new Mapping(getClass().getClassLoader()); if (getClass().getResource("eventoMapping.xml")==null) { System.out.println("RESOURCE IS NULL"); } else { System.out.println("RESOURCE IS OK"); } try { eventoMapping.loadMapping(getClass().getResource("eventoMapping.xml")); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (MappingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //Then we get a string with your XML //This is a StringReader, in this case I load it with a string I got from a dbase field. You can open // the file or use a getResource call for a file // some comments are in spanish // the important line is right after abracadabra.... // It creates the object with your questions as elements to the bean // basically Castor does all the heavy weight text to object work and you get a finished object to // show on the page // castor supports Collections so you can create various <question> tags in your // XML and they'll all be pumped into the Collection (list for example) in the way they're // in the page // read into http://www.castor.org/ and http://xstream.codehaus.org/ // CategoriasEventos translates to Event Categories, so it is pretty much what you need except categories are now questions // agarro el conejo y lo meto al sombrero StringReader XMLReader=new StringReader(XMLString.toString()); // creo varita magica Unmarshaller unmarshaller; try { // abracadabra.... unmarshaller = new Unmarshaller(eventoMapping); // SHAZAM!!! nuevo objeto a partir del XML CategoriasEvento eventoUnmarshalled=(CategoriasEvento) unmarshaller.unmarshal(XMLReader); // imprimo algo para ver como salio todo System.out.println("EVENTO ID IS "+eventoUnmarshalled.getId()+" lang is "+eventoUnmarshalled.getLang()); } catch (MarshalException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ValidationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MappingException e) { // TODO Auto-generated catch block e.printStackTrace(); }
|
 |
Vivian Ryder
Greenhorn
Joined: Nov 12, 2004
Posts: 20
|
|
You can not use a simple IO Reader like BufferedReader in servlets? And just out of curiosity, what is the method whereby XMLString was retrieved? [ November 08, 2005: Message edited by: Vivian Ryder ]
|
 |
 |
|
|
subject: Problem: read text file in java
|
|
|