zoukankan      html  css  js  c++  java
  • Java解析文本

    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<String> ingredients = new HashSet<String>();
           String ss = br.readLine();
         
           while((str = br.readLine()) != null && !(str.length() == 0)) {
            String[] strArr = str.split(" ");
            for(int i = 0;i<strArr.length;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<Dishes> list = new ArrayList<Dishes>();
          File file = new File("D:/dishes.txt");
          list = fm.parseFile(file);
          
          for(int i = 0; i<list.size();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<String> Categories;    //菜肴类别
     private Set<String> ingredients;   //菜肴配料
     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
     public Set<String> getCategories() {
      return Categories;
     }
     public void setCategories(Set<String> categories) {
      Categories = categories;
     }
     public Set<String> getIngredients() {
      return ingredients;
     }
     public void setIngredients(Set<String> ingredients) {
      this.ingredients = ingredients;
     }
    }
    使用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里

    http://zhidao.baidu.com/link?url=eZtAHGCL3EM3OrpBdzKj6LAWnxZSQkG7PzUqZ7BT347wuyuI_VKCv34Mz7j6XsC0CVf17lo4OJ2oYvmV36rvaq
  • 相关阅读:
    《ActionScript 3 CookBook 简体中文完整版》下载
    打开组件服务超慢,打不开属性窗口。
    无法引用Microsoft.Office.Interop.Excel的解决
    HttpWebResponse类
    反射性能优化 标记个
    配置文件入门 WebConfig.config常用配置节点介绍
    配置文件的读写
    HTTP权威指南阅读记录 第一章
    IIS
    锁机制与原子操作 <第四篇>
  • 原文地址:https://www.cnblogs.com/peterpanzsy/p/4017984.html
Copyright © 2011-2022 走看看