用java学生数据存储程序

用java编写学生数据存储程序

  • 要求:编写一个student类用来描述学生对象,创建若干学生,将其写入文件;再从文件读出学生信息,展示在屏幕上。
  • 代码:
  • student类:
//student.java
public class student {
    private String id;
    private String grade;
    private String name;

    public student(String id, String name, String grade) //构造器
    {
        this.id = id;
        this.grade = grade;
        this.name = name;
    }

    public String return_id() //返回学号
    {
        return id;
    }

    public String return_information() //返回所有信息
    {
        return "学号:"+id+"\t年级:"+grade+"\t姓名:"+name;
    }
}

  • 主类:
//WriteRead.java
import java.io.*;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Pattern;

public class WriteRead{
    public static String id_str; //要写入文件,所以转换为String类型
    public static String grade_str;
    public static String name;
    public static void main(String[] args) throws IOException{
        System.out.print("请输入录入学生人数:");
        int stu_num = get_num();
        student []stu = new student[stu_num]; //创建一个学生数组,保存学生信息
        for (int i = 0; i < stu_num; i++)
        {
            get_information();
            for (int j = 0; j < i; j++)
            {
                while (id_str.equals(stu[j].return_id()))
                {
                    System.out.println("学号重复,请重新输入!");
                    get_information();
                }
            }
            student s = new student(id_str, name, grade_str);
            stu[i] = s;
        }

        File file = new File("Student.txt"); //创建一个文件对象file
        WriteFile(file, stu);
        ReadFile(file);
    }
    
    public static int get_num() //获取录入学生信息的人数
    {
        try {
            Scanner sc = new Scanner(System.in);
            int stu_num = sc.nextInt();
            if (stu_num < 0) {
                System.out.println("请输入正整数!");
                return get_num();
            }
            return stu_num;
        } catch (InputMismatchException e) {
            System.out.println("请输入整数!");
            return get_num();
        }
    }
    
    public static void get_information() //获取信息
    {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入学号:");
            int id = sc.nextInt();
            System.out.print("请输入年级:");
            int grade = sc.nextInt();
            System.out.print("请输入姓名:");
            name = sc.next();

            Pattern pattern = Pattern.compile(".*\\d+.*");
            id_str = String.valueOf(id); //int类型转换为String类型
            grade_str = String.valueOf(grade);
            if (pattern.matcher(name).matches())
            {
                System.out.println("名字不能为数字,请重新输入!");
                get_information();
            }
        }
        catch (InputMismatchException e)
        {
            System.out.println("输入非法,请重新输入!");
            get_information();
        }
    }

    public static void WriteFile(File file, student []stu) throws IOException //写入文件
    {
        FileWriter fw = new FileWriter(file,false); //要覆盖之前的内容,选择false
        BufferedWriter bw = new BufferedWriter(fw);

        for (student s : stu)
        {
            bw.write(s.return_information());
            bw.newLine(); //写入一个学生的信息后换行
        }
        bw.close(); 
        fw.close();
    }

    public static void ReadFile(File file) throws IOException //读取文件
    {
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String s;
        while ((s = br.readLine()) != null)
        {
            System.out.printf(s); //逐行打印结果,printf是为了输出\t
        }
        br.close();
        fr.close();
    }
}

  • 运行结果:
    用java学生数据存储程序_第1张图片
    用java学生数据存储程序_第2张图片

  • 文件读取与写入方法参考:JAVA读取与写入文件

你可能感兴趣的:(Java,java)