Java解析复杂文本
关注:238 答案:4 mip版
解决时间 2021-01-30 03:36
提问者碎情漂移ド
2021-01-29 14:08
请高手帮帮忙,想了几天也没想到办法,在线等。使用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 15 oz Raisins
2 c Jam 1 c Nutmeats
2 ts Baking soda 1 x Figs and dates as desired
这个也应该需要解析至少存在一个数组或者list里
最佳答案
二级知识专家抚弦思华年
2021-01-29 15:28
import java.io.*;
import java.util.*;
public class FileManager {
public List parseFile(File file) {
String str = null;
List list = new ArrayList();
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 categories = new HashSet();
for(int j = 0;j
categories.add(cateArr[j]);
}
Set ingredients = new HashSet();
String ss = br.readLine();
while((str = br.readLine()) != null && !(str.length() == 0)) {
String[] strArr = str.split(" ");
for(int i = 0;i
ingredients.add(strArr[i]);
}
}
Dishes d = new Dishes();
d.setName(dishName);
d.setCategories(categories);
d.setIngredients(ingredients);
list.add(d);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FileManager fm = new FileManager();
List list = new ArrayList();
File file = new File("D:/dishes.txt");
list = fm.parseFile(file);
for(int i = 0; i
System.out.println(list.get(i).getName());
System.out.println(list.get(i).getCategories());
System.out.println(list.get(i).getIngredients());
}
}
}
//菜肴类,一种菜对应于菜肴类的一的实例
class Dishes {
private String name;
private Set Categories; //菜肴类别
private Set ingredients; //菜肴配料
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getCategories() {
return Categories;
}
public void setCategories(Set categories) {
Categories = categories;
}
public Set getIngredients() {
return ingredients;
}
public void setIngredients(Set ingredients) {
this.ingredients = ingredients;
}
}
全部回答
1楼一个超人
2021-01-29 17:45
我想:使用ini xml等存放多好啊
2楼孤者何懼
2021-01-29 17:04
public class Bean {
private String Name;
private String Categories;
private String peiliao;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getCategories() {
return Categories;
}
public void setCategories(String categories) {
Categories = categories;
}
public String getPeiliao() {
return peiliao;
}
public void setPeiliao(String peiliao) {
this.peiliao = peiliao;
}
}
import java.io.BufferedReader;
import java.io.FileReader;
public class Test {
public static void main(String[] args) {
Bean bean = new Bean();
try {
//声明输入流对象
FileReader fr = new FileReader("d://1.txt");
BufferedReader br = new BufferedReader(fr);
//获取第一行信息.截取name后面的值
String nameTemp = br.readLine().substring(6);
//保存到实体bean类
bean.setName(nameTemp);
//获取第二行信息.截取Categories后面的值
String CategoriesTemp = br.readLine().substring(12);
//保存到实体bean类
bean.setCategories(CategoriesTemp);
String peiliangTemp = br.readLine()+br.readLine()+br.readLine()+br.readLine();
bean.setPeiliao(peiliangTemp);
//输出实体类bean属性的值
System.out.println(bean.getName());
System.out.println(bean.getCategories());
System.out.println(bean.getPeiliao());
fr.close();
br.close();
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
自我感觉这个方法.好傻. 呵呵. 效果是达到了..
3楼雨落轻尘
2021-01-29 16:16
可以试试正则表达式,字符串split打散。
我要举报
如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
→点此我要举报以上信息!←
推荐资讯
大家都在看