Java解析复杂文本

来源:百度知道 编辑:UC知道 时间:2024/06/14 10:34:38
请高手帮帮忙,想了几天也没想到办法,在线等。使用JAVA对下面的复杂文本进行解析,这是一个菜谱的txt文本文件,下面只列出两道菜谱,其余格式都相同.只要能分别解析出Name,Categories,和下面的配料,存入一个对象中即可。请给出解析步骤的详细代码

大侠们拜托了,之后必定追加最高分.

下面是菜谱的文本格式

Name: Yu xiang rou si
Categories: Pork, Chinese

500 g Pork 10 g sugar
10 g chilly 1 1/2 g salt
15 g oil

In large bowl, blend oil and sugars on low until well mixed. Add chilly.
Beat in salt...

-----

Name: xi hong shi chao ji dan
Categories: tomato, Chinese

300 g tomato 10 g sugar
5 g oil 2 1/2 g salt

Chop tomatoes, blend oil and sugars on low until well mixed.
add some salt...

-----
配料部分是不确定的, 比如
2 1/2 c Brown sugar 2 ts Cloves
2 c Sour milk 2 ts Nutmeg
4 c Flour 3 ea Eggs, lg
3/4 c Butter

import java.io.*;
import java.util.*;

public class FileManager {

public List<Dishes> parseFile(File file) {
String str = null;
List<Dishes> list = new ArrayList<Dishes>();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while((str = br.readLine())!=null) {
if(str.startsWith("Name:")) {
int index = str.indexOf(":");
String dishName = str.substring(index+1,str.length());

str = br.readLine();
index = str.indexOf(":");
String categoriesString = str.substring(index+1,str.length());

String[] cateArr = categoriesString.split(",");
Set<String> categories = new HashSet<String>();
for(int j = 0;j<cateArr.length;j++) {
categories.add(cateArr[j]);
}

Set<Strin