2015-07-13 30 views
2

我有一個如下所示的文本文件。根據文件內容創建對象數組

LIST
年初
名稱:珍妮特
年齡:21
生日月份:四月

列表結尾

LIST
年初
名稱:彼得
年齡:34
生日月份:一月

LIST

年末所以我想抓住信息,並把它變成一個對象數組。它是一個廣泛的列表,我使用分隔符beginning of listend of list來分隔內容。

如何將這些項目存儲在對象數組中?

+0

究竟是完成這個任務的問題?不知道如何閱讀文件?或者如何解析一個字符串?或者如何創建列表?或者如何創建模型類? – Tom

+0

我可以讀取這些文件並創建一個模型類。我只需要知道如何基於「開始列表」和「結束列表」將它分開將會有很多無用的信息被讀取 –

回答

2

我建議你先創建一個類用於存儲信息,其中name,agebirthday month屬性。重寫toString()方法是一種非常好的做法,因此您可以整齊地打印出課程。

然後您可以檢查每行是否包含有關姓名,年齡或生日月份的信息,方法是將每行分成一個單詞數組並檢查信息。

一行顯示「END OF LIST」,您可以將參數class Person添加到ArrayList

對於我用「people.txt」作爲文件的例子(請確保您將文本文檔,其中包含您.java文件的SRC文件夾之外)。

Main.java

import java.io.*; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

public class Main { 

    public static void main(String[] args) 
    { 
     BufferedReader bufferedReader = null; 
     FileReader fileReader = null; 
     String name = null; 
     String age = null; 
     String month = null; 
     List<Person> people = new ArrayList<Person>(); 

     try 
     { 
      String fileName = "people.txt"; 
      fileReader = new FileReader(fileName); 
      bufferedReader = new BufferedReader(fileReader); 
      String line = null; 

      while ((line = bufferedReader.readLine()) != null) 
      { 
       String[] information = line.split(" "); 

       if (Arrays.asList(information).contains("Name:")) 
       { 
        name = information[1]; 
       } 
       if (Arrays.asList(information).contains("Age:")) 
       { 
        age = information[1]; 
       } 
       if (Arrays.asList(information).contains("month:")) 
       { 
        month = information[2]; 
       } 
       if (line.equals("END OF LIST")) 
       { 
        people.add(new Person(name, age, month)); 

        name = ""; 
        age = ""; 
        month = ""; 
       } 
      } 

      for (Person person : people) 
      { 
       System.out.println(person); 
       System.out.print("\n"); 
      } 
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println(e.getMessage()); 
     } 
     catch (IOException ex) 
     { 
      System.out.println("Error reading people.txt"); 
     } 
     finally 
     { 
      if (bufferedReader != null) 
      { 
       try 
       { 
        bufferedReader.close(); 
       } 
       catch (IOException ex) 
       { 
        System.out.println(ex.getMessage()); 
       } 
      } 
      if (fileReader != null) 
      { 
       try 
       { 
        fileReader.close(); 
       } 
       catch (IOException ex) 
       { 
        System.out.println(ex.getMessage()); 
       } 
      } 
     }  
    } 
} 

Person.java

public class Person { 
    private String name; 
    private String age; 
    private String birthday; 

    public Person(String name, String age, String birthday) 
    { 
     this.name = name; 
     this.age = age; 
     this.birthday = birthday; 
    } 

    @Override 
    public String toString() 
    { 
     String information = "Name: " + name + "\nAge: " + age + "\nBirthday: " + birthday; 
     return information; 
    } 
} 
+0

非常感謝您,您是上帝送來的! –

+0

@SantanaMax您的歡迎!如果您認爲我的回答很好,請點擊綠色的選中標記接受它。 –

+0

謝謝。只是一個簡單的問題。如果「行」爲空,那麼爲什麼我們使用空格分隔它,以及如何使用「開始列表」來劃分界限。列表之間會有很多無用的信息。 –