java 解析文本,用Java解析文本文件中的数据

本文介绍如何使用Java编写一个解析器,从类似'Mozart,W.A.;mymail1@mail.com'格式的文本文件中提取姓名和电子邮件地址。通过逐行读取并利用正则表达式或特定分隔符,实现数据的高效提取。
摘要由CSDN通过智能技术生成

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值