帮忙看一个题已知学生类含有实例属性(姓名、学号、成绩),实例方法(构造方法、getter方法)建立字符文件a.txt.第一行为一个整数(表示学生人数),第二行开始,每行提供一个学生所需的实例属性值。
  编写程序1,从a.txt输入学生人数和学生信息,建立学生数组。将所有学生数组各元素写入二进制文件a.dat (a.dat的开头4字节为表示学生数的整数)。
  编写程序2,从a.dat读出学生数据输出为字符文件b.txt。
  编写程序3,读入a.dat数据,用随机文件方式删除成绩<60的学生记录并存回a.dat

解决方案 »

  1.   


    楼主,把a.txt发我,我来完成
      

  2.   

    a.txt是自己建立的吧,帮帮忙了
      

  3.   

    数据文件:student.txt,内容如下:
    6
    张三,10001,67
    李四,10002,80
    王五,10003,92
    赵六,10004,58
    刘七,10005,66
    孙八,10006,84用于保存信息的Student类如下:
    package IO;public class Student {
    private String name;
    private String id;
    private int score;
    public Student(String name,String id,int score){
    this.name=name;
    this.id=id;
    this.score=score;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getId() {
    return id;
    }
    public void setId(String id) {
    this.id = id;
    }
    public int getScore() {
    return score;
    }
    public void setScore(int score) {
    this.score = score;
    }
    @Override
    public String toString() {
    return this.getName()+","+this.getId()+","+this.getScore();
    }
    }
    主类如下:
    package IO;import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;public class StudentIOMain {
    Student[] student;
    public static void main(String[] args) {
    new StudentIOMain();
    }
    public StudentIOMain(){
    readStudentTxt("student.txt");
    for(int i=0;i<student.length;i++)
    System.out.println(student[i]);
    writeStudentDat("student.dat");
    readStudentDat("student.dat");
    writeStudentTxt("student2.txt");

    }
    //读取文本文件中的学生信息
    public void readStudentTxt(String fileName){
    try {
    BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
    int len=Integer.parseInt(br.readLine().trim());
    student=new Student[len];
    for(int i=0;i<student.length;i++){
    String[] str=br.readLine().split(",");
    String name=str[0];
    String id=str[1];
    int score=Integer.parseInt(str[2].trim());
    student[i]=new Student(name,id,score);
    }
    br.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    System.out.println("文件读取失败!");
    }
    }
    //读取二进制文件中的学生信息
    public void readStudentDat(String fileName){
    try {
    DataInputStream dis=new DataInputStream(new FileInputStream(fileName));
    int len=dis.readInt();
    student=new Student[len];
    for(int i=0;i<student.length;i++){
    String str1=dis.readUTF();
    String[] str2=str1.split(",");
    String name=str2[0];
    String id=str2[1];
    int score=Integer.parseInt(str2[2]);
    student[i]=new Student(name,id,score);
    }
    dis.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    //将学生信息写入到文本文件
    public void writeStudentTxt(String fileName){
    try {
    BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
    bw.write(student.length+"\r\n");
    for(int i=0;i<student.length;i++)
    bw.write(student[i].toString()+"\r\n");
    bw.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    //将学生信息写入二进制文件
    public void writeStudentDat(String fileName){
    try {
    DataOutputStream dos=new DataOutputStream(new FileOutputStream(fileName));
    dos.writeInt(student.length);
    for(int i=0;i<student.length;i++){
    dos.writeUTF(student[i].toString());
    }
    dos.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }