Reading Values from a text file.  
Author Message
carlito





PostPosted: 2006-3-5 22:58:00 Top

java-programmer, Reading Values from a text file. 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





PostPosted: 2006-3-5 23:37:00 Top

java-programmer >> Reading Values from a text file. 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

 
carlito





PostPosted: 2006-3-5 23:47:00 Top

java-programmer >> Reading Values from a text file. 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





PostPosted: 2006-3-6 4:42:00 Top

java-programmer >> Reading Values from a text file. Andres, I believe what Ed wrote is exactly what you are requesting.

Opalinski
email***@***.com
http://www.geocities.com/opalpaweb/

 
 
Ed





PostPosted: 2006-3-6 5:41:00 Top

java-programmer >> Reading Values from a text file.
email***@***.com wrote:
> 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.

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 ...)