Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Reading Values from a text file.

Thread view: 
carlito@whitmore.fsnet.co.uk - 05 Mar 2006 14:57 GMT
Hi,

I have a problem reading in values from a text file. Basically i have a
webservice that gets queries from a database, and i wish to read the
queries in from a text file. I know how to read a text file but what i
really want to say is:

open file;
read first line of file;
use this line as query;
do query processing;
then read next line of query;
.....
.....
....
etc

what i would like to know is there a way of looping through the lines?

Many thanks

Andrew
Ed - 05 Mar 2006 15:36 GMT
Hmm, well, I think you're asking for something else, so I'll throw in
this solution so that you can say why it's not what you want (if that
makes any sense):

    try {
       File file = new File("myFileSomewhere");
       FileReader fileReader = new FileReader(file);
       BufferedReader br = new BufferedReader(fileReader);
       String line = null;
       boolean stillParsing = true;
       while (stillParsing) {
        line = br.readLine();
        if (line == null) {
           stillParsing = false;
        } else {
           processQuery(line);
        }
       }
       br.close();
       fileReader.close();
    } catch (Throwable t) {
       }

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition
carlito@whitmore.fsnet.co.uk - 05 Mar 2006 15:46 GMT
Thats sort of what i want Except say i have a text file that has the
following lines in it:

dog
cat
mouse
rat

i want these to be separte queries. and want something like what you
said but.

each pass that the loop makes i want it to read the firstline then
query database.
Secondpass read the second line and query database with that
string......etc.
so what i am looking for really is how to say:

readline 1
use line 1 as query
perform query
readline 2
use line 2 as query
perform query
....
....
if line equals null then no more queries

Appreciate the help

Andrew
opalpa@gmail.com opalinski from opalpaweb - 05 Mar 2006 20:41 GMT
Andres, I believe what Ed wrote is exactly what you are requesting.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Ed - 05 Mar 2006 21:40 GMT
> Thats sort of what i want Except say i have a text file that has the
> following lines in it:
[quoted text clipped - 5 lines]
>
> i want these to be separte queries.

If I understand you right, then the code snippet posted should have
been what you asked for; particularly, be aware that the BufferedReader
read's in text line-by-line (rather than character-by-character or the
entire file at one go); so on first iteration, the read line will be,
"dog," then on the next, it will be, "cat," etc.

.ed (listening to the absolutely gorgeous sound-track, "The Village;"
that man can play a violin ...)

--
www.EdmundKirwan.com - Home of The Fractal Class Composition


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.