how to parse text files, where order matters
I'm having trouble determining a way to parse a given text file.
Here is an entry in the file:
type = "book"
callnumber = "1"
authors = "a"
title = "t"
publisher = "p"
year = "2023"
each entry is separated by a line of whitespace (newline character).
so i have these variables (type, callnumber, authors, title....), and need to read this text and determine what values to set them to. For example, when 开发者_如何学运维i read the line "callnumber = 1", then I need set that variable to 1.
This is what I have so far. I read in a line at a time, so type = "book"
for example, and then I split that line into an array of strings, with the delimiter being ", so the array would contain type =
and book
.
Now my problem comes in going further from there. I figured I could cycle through each string in the array, character by character, until I hit whitespace. So i would have type
, but I don't have any data yet to store in type, and the grab will give me book
(ignoring the = and whitespace), but how can I attribute book to type?
In summary, I'm looking for a way to parse a text file line by line, and assign variables values, based on the words I find.
Thanks.
Ignoring the current route, why not make use of Properties.load(InputStream inputStream)
Properties properties = new Properties();
properties.load(new FileInputStream("filename"));
string type = properties.getProperty("type");
System.out.println(type);
book
I agree you should take the Properties route if your requirements allow you to. The next best option would be to deal with each line individually through a regular expression.
String type = "default";
int callnumber = 0;
String line = "type = \"book\"";
// String line = "callnumber = \"1\"";
Pattern linePattern = Pattern.compile("(\\w*) = \"(.*)\"");
Matcher matcher = linePattern.matcher(line);
if ( !matcher.matches() ) {
System.err.println("Bad line");
}
String name = matcher.group(1);
String value = matcher.group(2);
if ( "type".equals(name) ) {
type = value;
} else if ( "callnumber".equals(name) ) {
callnumber = Integer.parseInt(value);
} //...
In your case you would want to integrate this into your while loop that reads from the file, and replace line
with the line you've just read from the file.
To add to Aaron's solution:
Properties.load(new FileInputStream("<fileName>"));
will load the properties and to get any particular property,
use for example,
Properties.getProperty("type")
will give you string "book".
Is the order of the variables in the text file always going to be the same?
I'm guessing you wouldn't be asking if that was the case.
Why not just make a method:
void assignVariableByName(String name, <type> value) {
if(name.contains("type"))
type = value;
else if(name.contains("callnumber"))
callnumber = value;
}
Then usage ->
You have the array of strings you split... and you call
assignVariableByName(parsedLine[0],parsedLine[1]);
Assigning values to variables has probably been done elsewhere more cleanly. If you want to 'tokenize' your string however, use a string tokenizer.
The new school is to use the split method of the String class.
token[] = line.split("\s++")
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)
Below is the old school way:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
While(String line = someInput.readLine())
StringTokenizer st = new StringTokenizer(line)
while(st.hasMoreTokens)
{
String token = st.nextToken()
//branch on token command, skip token '=', and assign on values
}
精彩评论