Current location - Recipe Complete Network - Complete cookbook of home-style dishes - Java parsing complex text
Java parsing complex text
Import java.io. *;

Import java.util. *;

Public class FileManager {

Public List & lt Dishes & gt Parsing File (File File) (

String str = null

List & lt dishes & gtlist = new ArrayList & lt dishes & gt ();

Try {

buffered reader br = new buffered reader(new file reader(file));

while((str = br.readLine())! =null) {

if(str.startsWith("Name:")) {

int index = str . index of(":");

string dish name = str . substring(index+ 1,str . length());

str = br . readline();

index = str . index of(":");

string categories string = str . substring(index+ 1,str . length());

string[]cateArr = categories string . split(",");

Set< string & gt categories = new hashset & lt. String & gt ();

for(int j = 0; j & ltcate arr . length; j++) {

categories . add(cateArr[j]);

}

Set< string & gt ingredient = new hash set< string & gt ();

string ss = br . readline();

while((str = br.readLine())! = null & amp& amp! (str.length() == 0)) {

string[]strArr = str . split(" ");

for(int I = 0; I & ltstrArr.lengthi++) {

ingredients . add(strArr[I]);

}

}

D = new dish ();

d . set name(dish name);

D.setCategories;

D.setIngredients;

list . add(d);

}

}

} catch (IOException e) {

// TODO automatically generated catch block

e . printstacktrace();

}

Return order;

}

Public static void main(String[] args) {

// TODO automatically generated method stub

file manager FM = new file manager();

List & lt dishes & gtlist = new ArrayList & lt dishes & gt ();

File File = new File(" D:/disks . 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))。 get categories());

System.out.println(list.get(i))。 geting redients());

}

}

}

//Cuisine, an example where one cuisine corresponds to one cuisine.

Category dishes {

Private string name;

Private settings & lt string & gt category; //Food category

Private settings & lt string & gt ingredients; //Dish ingredients

Public string getName() {

Returns the name;

}

Public void setName {

this.name = name

}

Public Settings & lt String & gtgetCategories() {

Return category;

}

Public void set categories (set <: String & gt category) (

Category = category;

}

Public Settings & lt String & gtgetIngredients() {

Return ingredients;

}

public void Set ingredients(Set & lt; String & gt Ingredients) (

This.ingredients = ingredients;

}

}