java 學習之路----java開發實戰經典反射習題答案(15章)


答案:


class Student{
private String name;
private String age;
private String score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public Student() {
}


}


public class FSTest {
public static void main(String[] args)throws Exception {
DataOutputStream out=new DataOutputStream(new FileOutputStream(new File("f:"+File.separator+"a.txt")));
DataInputStream in=new  DataInputStream(new FileInputStream(new File("f:"+File.separator+"a.txt")));
System.out.println("請輸入學生信息:(like:20:100)");
boolean Y=true;
String str = "";
Scanner scanner=new Scanner(System.in);
while(Y){


str+= scanner.next()+":";



System.out.println("是否繼續輸入:是:Y:否:N");
String a= scanner.next();
if(a.equalsIgnoreCase("Y")){
Y=true;
}
if(a.equalsIgnoreCase("N")){
scanner.close();
Y=false;
}
}
out.writeBytes(str);
int temp=0,len=0;
byte b[]=new byte[1024];
while((temp=in.read())!=-1){
b[len]=(byte) temp;
len++;

}
in.close();
out.close();
String s=new String(b);

String string[]=s.split("\\:");
Class<Student> student=(Class<Student>) Class.forName("FS.Student");
int j=0;
Student student2=null;
List<Student>list=new ArrayList<>();
for(int i=0;i<string.length;i++){

if((i+1)%3==1){
student2=student.newInstance();
student2.setName(string[i]);
}else if((i+1)%3==2){


student2.setAge(string[i]);
}else if((i+1)%3==0){
System.out.println(string[i]);

student2.setScore(string[i]);
list.add(student2);
}
}

for(Student s2:list){
System.out.println("姓名:"+s2.getName()+"  年齡:"+s2.getAge()+"  成績:"+s2.getScore());
}

}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章