有一个test.txt 文件,里面是这样存的:
菜名和数量间以Table分开,每行菜名前面可能还有还有空格;
请问当我用BufferdReader一行一行处理的时候,怎么把里面的时间和对应的人,以及他预定菜名和数量一个一个取出来,从而给这个人设置这些属性值呢?
有一个test.txt 文件,里面是这样存的:
菜名和数量间以Table分开,每行菜名前面可能还有还有空格;
请问当我用BufferdReader一行一行处理的时候,怎么把里面的时间和对应的人,以及他预定菜名和数量一个一个取出来,从而给这个人设置这些属性值呢?
这个问题的核心就是,你逐行处理的时候,如果是以date开始的记录,就创建一个Person,其后就是这个Person相关的信息,直到遇到下一个date止。
即:每次遇到date就新建一个Person。我给你实现了完整的代码:Person类,Menu类,FileUtil类,保证你的FileUtil中你的fileName路径存在。
import java.util.List;
public class Person {
private String date;
private String name;
private List<Menu> menu;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Menu> getMenu() {
return menu;
}
public void setMenu(List<Menu> menu) {
this.menu = menu;
}
public String toString(){
return "Person:[date="+date+",name:"+name+",menus:"+menu+"]";
}
}
菜单;
public class Menu {
private String name;
private String count;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String toString(){
return "menu=[name:"+name+",count:"+count+"]";
}
}
最终的解析类:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FileUtil {
/**
* 读取文件内容,每一行作为一个记录存入列表中
*
* @param fileName
* @return
*/
public static List<String> readToList(String fileName) {
List<String> result = new ArrayList<String>();
if (fileName == null || "".equals(fileName)) {
System.out.println("文件名称为空...");
return result;
}
File file = new File(fileName);
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fin, "UTF-8"));
String content = reader.readLine();
while (content != null) {
result.add(content);
// 循环读取下一行,没有这行代码就是死循环的啦。。。
content = reader.readLine();
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* 解析文件的内容
* @param content
* @return
*/
public static List<Person> parseContent(List<String> list){
List<Person> persons = new ArrayList<Person>();
Person person = null;
for(int i =0;i<list.size();i++){
String value = list.get(i);
//如果是以date开始说明是日期,那么它其后就是name,新建一个Person
if(value.contains("date")){
person = new Person();
person.setMenu(new ArrayList<Menu>());
persons.add(person);
//date=xxx,用split解析到=后面的日期
person.setDate(value.split("=")[1]);
//date后面就是name
person.setName(list.get(i+1).split("=")[1]);
//此操作处理了两行记录,下标加一
i++;
}else{
//不是以date开始,说明是菜单,以空格分隔,将菜单添加到Person的菜单列表中
String[] menuInfo = value.split(" ");
Menu menu = new Menu();
menu.setName(menuInfo[0].trim());
menu.setCount(menuInfo[1].trim());
//将该菜单添加到这个人的菜单列表下面
person.getMenu().add(menu);
}
}
return persons;
}
public static void main(String[] args) {
String filePath = "src/com/menu/menu.txt";
List<String> list = readToList(filePath);
List<Person> persons = parseContent(list);
for(Person p:persons){
System.out.println(p);
}
}
}
我处理时先将整个文件读取成List<String>列表,然后逐个处理列表的。就是每次碰到date就新建一个Person,那么在遇到下一个date之前的记录都是这个Person的菜单信息了。你可以测试看看。有问题再沟通。