I'm trying to create a parser in Java that would help me to get some details from a text file.
The data in the file looks like this, but with more entries:
.
http://www.someurl1.com/
PERSONAL ADDRESS: Mozart, W.A.; Some address 1, Austria; email: mymail1@mail.com
.
http://www.someurl2.com/
PERSONAL ADDRESS: Beethoven, L.V.; Some address 2, Germany; email: mymail2@mail.com
As you can see, the data always respects a pattern, and what I would like to get is just the name and the e-mail for every entry. A possible good output would be this:
Mozart, W.A. ; mymail1@mail.com
Beethoven, L.V. ; mymail2@mail.com
Every entry starts with a . followed by a space in the first line. Then in the next line above the dot, there's the URL. In the following line, there's more data: name, address and e-mail, all separated by a ;.
This isn't hard but I'm having some troubles starting. I've created a Main class in which I read the text file to a String. But then I really don't know what's the best way to parse something like this in Java, if I should try to use regular expressions or just get looking for the ;.
解决方案
Read in the text file line by line and then do an action based on that line.
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
// process the line.
if (line.equals(". "))
{
// Do something with first line
line = br.readLine()
// Do something with second line
line = br.readLine()
// Split up the third line by space
String split[]= StringUtils.split(line); // split[1] = "Mozart," so you may need to do a little more work there
}
}
br.close();