Home > Archive > Java Help > August 2005 > java newbie, parse an input file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
java newbie, parse an input file
|
|
| boanator@optonline.net 2005-08-17, 10:02 pm |
| I am new to java and I am trying to determine the best way to parse an
input file to extract data.
// example input file:
....
token1=someData
token2=someMoreData
token3=evenMoreData
....
I want to have a function, getField(String token), that will parse the
file and extract data depending on the given token. example:
System.out.println( getField("token1=") );
System.out.println( getField("token3=") );
The previous print statements should display:
someData
evenMoreData
There has to be a nice clean easy way to do this in java. I'm just
having trouble determining which would be the best & fastest way to
extract that data.
Any advice would be appreciated. Thanks in advance.
| |
| Kenneth P. Turvey 2005-08-17, 10:02 pm |
| boanator@optonline.net wrote:
[Snip]
> token1=someData
> token2=someMoreData
> token3=evenMoreData
> ...
[Snip]
> There has to be a nice clean easy way to do this in java. I'm just
> having trouble determining which would be the best & fastest way to
> extract that data.
>
> Any advice would be appreciated. Thanks in advance.
This looks like homework. You should look at Properties:
From google:
http://builder.com.com/5100-6370-1049436.html
--
Kenneth P. Turvey <kt@squeakydolphin.com>
| |
| Jeff Schwab 2005-08-17, 10:02 pm |
| boanator@optonline.net wrote:
> I am new to java and I am trying to determine the best way to parse an
> input file to extract data.
> // example input file:
> ...
> token1=someData
> token2=someMoreData
> token3=evenMoreData
> ...
>
> I want to have a function, getField(String token), that will parse the
> file and extract data depending on the given token.
You might consider reading one line at a time, and calling
line.split("=", 2) for each line. You could treat the elements of the
resulting arrays as key/value pairs, and add them to the Map of your choice.
| |
| boanator@optonline.net 2005-08-18, 6:00 pm |
| I know this looks like homework, but as I said I am a newbie.
I don't want to read the file once and set all of the key/value pairs
in memory b/c the values in this file can change often (I want to make
sure I have the latest values).
Right now I have a synchronized function that takes in the token name
and returns the value. (Note: each token exists in this file ONLY
once.):
synchronized public String getField(String token) throws MyException {
String tmpLine, retString=null;
int index, offset=token.length();
try {
// open the file and wrap it in a buffered reader
File file = new File("data.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
// search for the given token
while ( (tmpLine=br.readLine()) != null ) {
index = tmpLine.indexOf(token);
if (index != -1) {
retString = tmpLine.substring(index+offset);
break;
}
br.close();
} catch (IOException e) {
throw new MyException(e.getMessage());
}
if(retString == null) {
throw new MyException(token+" not found in file: data.txt");
} else {
return retString;
}
}
So, If the file looks like the following:
// data.txt
....
token1=someValue
token2=someOtherValue
token3=oneMoreValue
....
And I make the following print request:
System.out.println(getField("token2="));
The output should be:
someOtherValue
I don't think the Properties suggestion is the best way to go. It
seems like a lot of work considering the file already exists and I just
want to pull out some values every now and then.
I am just wondering if there is a cleaner and/or faster way to parse
the file to pull out the values. Also, when I open the file for
parsing, is there a way to check if some other application already has
this file open?
| |
| Oliver Wong 2005-08-18, 6:00 pm |
| <boanator@optonline.net> wrote in message
news:1124370683.694329.92190@o13g2000cwo.googlegroups.com...
> I am just wondering if there is a cleaner and/or faster way to parse
> the file to pull out the values.
Cache the values. Check if the file has been modified on each call. If
so, update the cache. Return the value from the cache.
- Oliver
|
|
|
|
|