日期:2014-05-20 浏览次数:20886 次
package a; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; class Student{ private int id ; private String name ; private int age ; private int sex; private String phone ; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Student() { } public Student(int id, String name, int age, int sex, String phone) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; this.phone = phone; } public Student(String[] arr){ if (arr.length == 5){ this.id = Integer.valueOf(arr[0]).intValue() ; this.name = arr[1] ; this.age = Integer.valueOf(arr[2]).intValue() ; this.sex = Integer.valueOf(arr[3]).intValue() ; this.phone = arr[4] ; } } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", phone=" + phone + "]"; } } public class ReadStudent { /** * @param args * @author sunstar * @throws FileNotFoundException * @date 2012-7-4 上午11:14:15 */ public static void main(String[] args) { // TODO Auto-generated method stub List list = readStudent("D:\\std.txt") ; Iterator it = list.iterator() ; Student std = null ; System.out.println("========输出结果========") ; while(it.hasNext()){ std = (Student) it.next() ; System.out.println(std) ; } } private static List readStudent(String fileName) { BufferedReader br = null ; String str = "" ; List list = new ArrayList() ; String tmp ; Student std = null ; try { br = new BufferedReader(new FileReader(fileName)); while ((str = br.readLine()) != null) { tmp = str.replaceAll("string----------", "") ; System.out.println(str + " === " + tmp) ; std = new Student(tmp.split(";" )) ; System.out.println(std) ; list.add(std) ; } } catch (Exception e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } return list ; } }