student.txt文件里有存有
id int
name char(20)
age int
读出来之后根据属性,怎么能让他自动创建一个对象。
自动封装成这样的一个类
plblic class student{
private int id;
private string name;
private int age
}
可以看看javacompiler类
你可以在Student类里写上从文件创建的方法:
123
小明
则:
[code="java"]
public class Student {
public static Student createStudent(File file) {
try {
BufferedInputReader ir = new BufferedInputReader(
new InputStreamReader(new FileInputStream(file)));
String s = ir.readLine();
final int id = Integer.parseInt(s);
s = ir.readLine();
final String name = s;
s = ir.readLine();
final int age = Integer.parseInt(s);
return new Student(id, name, age);
} catch (IOException ioe) {}
return null;
}
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
[/code]