1、求 2~200 之间的所有素数,将求得的结果保存到 PRIME.TXT 文件中。
2、输入 5 个学生的信息(包含学号、姓名、3 科成绩),统计各学生的总分,然后将学生信息和统计结果存入二进制数据文件 STUDENT.DAT 中。
3、从第 2 题中建立的STUDENT.DAT 文件中读取数据,寻找平均分最高的学生,并输出该学生的所有信息。
注意:编程时应考虑异常处理
1.
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Prime {
public static void main(String[] args) throws Exception {
File f=new File("F:"+File.separator+"PRIME.txt");
try {
f.createNewFile();
} catch (IOException e) {
// TODO: handle exception
System.err.println(e);
}
Writer out = new FileWriter(f,true);
for(int i=2;i<=200;i++) {
boolean flag=true;
for (int j=2;j<=Math.sqrt(i);j++) {
if(i%j==0) {
flag=false;
break;
}
}
out.write(i+"t"+"");
}
out.close();
}
}
2-3.
Stu类
import java.io.Serializable; public class Stu implements Comparable,Serializable{ private int id; private String name; private int chenese_score; private int math_score; private int english_score; private int all_score; @Override public int compareTo(Stu o) { // TODO 自动生成的方法存根 if(this.avg_score>o.avg_score) { return -1; }else if(this.avg_score==o.avg_score){ return 0; }else { return 1; } } private double avg_score; public Stu(int id,String name,int chenese_score,int math_score,int english_score) { this.id=id; this.name=name; this.chenese_score=chenese_score; this.math_score=math_score; this.english_score=english_score; // TODO 自动生成的构造函数存根 } @Override public String toString() { return "Stu [id=" + id + ", name=" + name + ", chenese_score=" + chenese_score + ", math_score=" + math_score + ", english_score=" + english_score + ", all_score=" + all_score + ", avg_score=" + avg_score + "]"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getChenese_score() { return chenese_score; } public void setChenese_score(int chenese_score) { this.chenese_score = chenese_score; } public int getMath_score() { return math_score; } public void setMath_score(int math_score) { this.math_score = math_score; } public int getEnglish_score() { return english_score; } public void setEnglish_score(int english_score) { this.english_score = english_score; } public int getAll_score() { return this.chenese_score+this.english_score+this.math_score; } public void setAll_score() { this.all_score = this.chenese_score+this.english_score+this.math_score; } public void setAvg_score() { this.avg_score = this.all_score/3; } public double getAvg_score() { return (this.chenese_score+this.english_score+this.math_score)/3; } }
Test类
package demo5_15;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
public class Test{
public static void main(String[] args){
Stu[] ss=new Stu[5];
ss[0]=new Stu(01, "one", 90, 88, 89);
ss[1]=new Stu(02, "two", 88, 100, 70);
ss[2]=new Stu(03, "three",78, 78, 100);
ss[3]=new Stu(04, "four", 89, 98, 99);
ss[4]=new Stu(05, "five", 69, 78, 87);
//ss.add(s1);ss.add(s2);ss.add(s3);ss.add(s4);ss.add(s5);
File f=new File("F:"+File.separator+"STUDENT.DAT");
ObjectOutputStream dos=null;
try {
dos=new ObjectOutputStream(new FileOutputStream(f));
} catch (FileNotFoundException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
for(int i=0;i<5;i++) {
Stu t=ss[i];
t.setAll_score();//把avg 和 all set
t.setAvg_score();
}
Arrays.sort(ss);//调用重写的compareTo 按AVG排序
for(int i=0;i<5;i++) {
Stu t=ss[i];
try {
dos.writeObject(t);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
try {
dos.close();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
///第三题
ObjectInputStream ois=null;
try {
ois=new ObjectInputStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//输出avg最高
read_Stu(ois,ss);
}
/
public static void read_Stu(ObjectInputStream ois,Stu[] ss){//创建一个读avg最大学生信息的函数
Stu temp=null;
try {
temp=(Stu)ois.readObject();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println("输出平均分最高的同学:");
System.out.println(temp);
try {
ois.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
///
}
}



