用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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不要做码农呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值