• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem: read text file in java

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for replying fisrt!
yea, it's servlet. The file must be text file and put in my app directory.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic