练习主题:展示磁盘文件中的内容
文件中的内容:![](https://i-blog.csdnimg.cn/blog_migrate/0f5c0ee2b55746649bcafa80abdd1ae3.png)
实现功能代码:
package T;
import java.util.Date;
public class Student {
private String name;
private String sex;
private Date birthday;
private String address;
public Student(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
", address='" + address + '\'' +
'}';
}
}
package T;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
public class T04 {
public static void main(String[] args) throws IOException, ParseException {
//定义一个File对象,关联映射硬盘文件
File f1=new File("z:/a.txt");
//2.定义缓冲字符流
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
//3.对文档进行读取,每次读取一行数据
ArrayList<Student> list=new ArrayList<>();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
String temp;
while(true){
temp=br.readLine();//读取一行数据
if (temp!=null){
String[] split=temp.split("-");
Student student=new Student();
student.setName(split[0]);
student.setSex(split[1]);
student.setBirthday(sdf.parse(split[2]));
student.setAddress(split[3]);
list.add(student);
System.out.println(student);
}else{
break;
}
}
fr.close();
br.close();
}
}
运行后结果: