当前位置:网站首页>5.文件的读写(学生类)

5.文件的读写(学生类)

2022-06-22 15:02:00 小木荣

5 文件读写
定义学生类数组,有N个人(N=5),包括姓名和语数外三名课的成绩,实现学生数组的文件读写。

import java.io.*;
import java.util.Scanner;

public class Student {
    
	final static int N = 5;
	private String name;
	private int Chinese, English, math;

	public Student(String name, int Chinese, int English, int math) {
    
		this.name = name;
		this.Chinese = Chinese;
		this.math = math;
		this.English = English;
	}

	public static void main(String[] args) {
    
		Student[] stu = new Student[N];
		int num = 0;
		stu[0] = new Student("李1白", 99, 99, 99);
		stu[1] = new Student("李2白", 89, 89, 90);
		stu[2] = new Student("李3白", 120, 120, 60);
		stu[3] = new Student("李4白", 120, 120, 60);
		stu[4] = new Student("李5白", 120, 120, 60);
		File fp = new File("student.txt");
		try {
    
			PrintWriter output = new PrintWriter(fp);
			for (int i = 0; i<N; i++) {
    
				output.print(stu[i].name + ' ');
				output.print("语文:" + stu[i].Chinese);
				output.print("数学:" + stu[i].English);
				output.println("英文:" + stu[i].math);
				num++;
			}
			output.close();
		} catch (IOException e) {
    
			System.out.println(e.toString());
		}
		try {
    
			String stus = null;
			Scanner input = new Scanner(fp);
			for (int i = 0; i < num; i++) {
    
				stus = input.nextLine();
				System.out.println(stus);
			}
			input.close();
		} catch (IOException e) {
    
			System.out.println(e.toString());
		}
	}
}
原网站

版权声明
本文为[小木荣]所创,转载请带上原文链接,感谢
https://blog.csdn.net/W2001r/article/details/117335698