- package com.heima.text;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.util.ArrayList;
- import java.util.List;
- public class Demo4 {
- public static void main(String[] args) throws Exception {
-
- List<Person> al = new ArrayList<>();
- BufferedReader buf = new BufferedReader(new FileReader("a.txt"));
-
- String line = null;
- while((line = buf.readLine()) != null) {
-
- String[] strArr2 = new String[2];
- int index = 0;
- String[] strArr = line.split(":"); //分割字符串 age=25 name=张山
- for(String s : strArr) {
- strArr2[index++] = s.replaceAll("[a-z]{3,4}=", ""); //获取属性值 将属性值存入数组strAArr2中
- }
- al.add(new Person(strArr2[1], Integer.parseInt(strArr2[0]))); //添加Person对象
-
- }
- //遍历集合
- for(Person p : al) {
- System.out.println(p);
- }
- buf.close();
- }
- }
- class Person { //bean类
- private String name;
- private int age;
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- 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;
- }
- @Override
- public String toString() {
- return "Person [name=" + name + ", age=" + age + "]";
- }
-
- }
复制代码 |