public Student[] readFile(String path) {
String str = null;
String[] array = null;
Student[] stus = new Student[4]; //Student 持久化对象类
int i = 0;
FileReader file = null;
BufferedReader read = null;
try {
file = new FileReader(path); //path 指定文件的路径
read = new BufferedReader(file);
while ((str = read.readLine()) != null ) {
array = str.split("-"); //采用自定义的分割符分割字符串
Student stu = new Student(); //利用临时的Student对象存储一条数据记录
stu.setName(array[0]);
stu.setAge(Integer.parseInt(array[1]));
stu.setSex(array[2]);
stus[i++] = stu;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
read.close();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return stus
}
转载于:https://www.cnblogs.com/fuq1109/archive/2013/03/06/2946058.html