我有一大堆电子邮件,我需要从中提取信息。我最近在一个网站上存储了电子邮件中客户的所有联系信息。他们想要开始将其存储在数据库中。我正在使用Java来尝试提取这些信息。我有点卡住了。
我已经能够自己加载电子邮件,但无法提取信息。这是一封电子邮件示例:
> ----------------------------------------------------------------------
> Name: Person's Name
> Phone:=20
> Email: test@testperson.com
> Street:=20
> City:=20
> State:=20
> Zip:=20
> Country:=20
> Arrival: 15 Nov 2010
> Departure: 22 Nov 2010
> Message: This is a message
> ----------------------------------------------------------------------
> Name: Second Person
> Phone:=555-5554
> Email: test@testpsdf.com
> Street:=1234 Main St.
> City:=20
> State:=20
> Zip:=23412
> Country:=20
> Arrival: 15 Nov 2010
> Departure: 22 Nov 2010
> Message: This is a message
> ----------------------------------------------------------------------
我需要拉到没有=20
的地方。我需要以某种方式将所有这些信息放入表格或CSV文件中,以便将其导入mysql数据库。
编辑:
这实际上是文件看起来更像
> ----------------------------------------------------------------------
> Name: Erin
> Phone: 401-
> Email: eri
> Street: 737
> City: Paw
> State:
> Zip: 02
> Country: USA
> Arrival: 17 Jul 2011
> Departure: 23 Jul 2011
> Message: I .=20
> ----------------------------------------------------------------------
>=20
> A representative will be in touch shortly.
> Thank You,
>
>=20
Begin forwarded message:
> From:
> Date: July 8, 2010 12:35:13 PM EDT
> To:
> Subject: Thank you for completing our contact form!
>=20
> Thank you for completing our contact form! We received the following =
information from you:
> ----------------------------------------------------------------------
> Name: Ludd
> Phone:=20
> Email: aedu
> Street: 25
> City: Signal
> State:
> Zip:
> Country: USA
> Arrival: 25 Nov 2010
> Departure: 30 Nov 2010
> Message: Not sure if
> ----------------------------------------------------------------------
>=20
> A representative will be in touch shortly.
> Thank You,
>
>=20
Begin forwarded message:
> From:
> Date: July 8, 2010 11:29:49 AM EDT
> To:
> Subject: Thank you for completing our contact form!
>=20
> Thank you for completing our contact form! We received the following =
information from you:
> ----------------------------------------------------------------------
> Name: Stephanie
> Phone: 41
> Email: sgor
> Street: 2-
> City:
> State: On
> Zip: 1J6
> Country:
> Arrival: 18 Aug 2010
> Departure: 21 Aug 2010
> Message:=20
> ----------------------------------------------------------------------
>=20
> A representative will be in touch shortly.
> Thank You,
>=20
Begin forwarded message:
> From:
> Date: July 8, 2010 11:16:36 AM EDT
> To:
> Subject: Thank you for completing our contact form!
>=20
> Thank you for completing our contact form! We received the following =
information from you:
> ----------------------------------------------------------------------
> Name: Stacey
> Phone: 001
> Email: staceymou
> Street: 60
> City: New York
> State: NY
> Zip: 0
> Country: USA
> Arrival: 10 Dec 2010
> Departure: 14 Dec 2010
> Message: Looking to reserve
> ----------------------------------------------------------------------
答案 0 :(得分:2)
以下是一种将所有此类标头提取到Map<String, String>
的方法。它使用Google的Guava library来简化事情:
public static Map<String, String> readValuesFromFile(final File f)
throws IOException{
final Splitter splitter =
Splitter.on(':').trimResults().omitEmptyStrings();
final Map<String, String> map = Maps.newHashMap();
for(final String line :
Lists.transform(
Files.readLines(f, Charsets.UTF_8),
new Function<String, String>(){
@Override
public String apply(final String input){
return input != null && input.startsWith("> ")
? input.substring(2)
: input;
}
})){
if(line.startsWith("---")){
break;
}
final String[] items =
Iterables.toArray(splitter.split(line), String.class);
if(items.length == 2 && !items[1].startsWith("=20")){
map.put(items[0], items[1]);
}
}
return map;
}
答案 1 :(得分:0)
阅读文件,直至获得“&gt; -------”行。
阅读每一行(BufferedReader.readLine()
),在其中找到“:”的位置,在该行前后使用该行的一部分(使用String.indexOf()
,String.substring()
,String.trim()
)。现在您有一个字段名称及其值。除非值为“= 20”,否则请将其放入数据库或CSV记录中。
如果再次遇到“&gt; -------”行,则记录结束。您可以通过其中没有“:”来轻松检测到它。