当前位置:网站首页>5. reading and writing of documents (students)

5. reading and writing of documents (students)

2022-06-22 16:18:00 Xiaomurong

5 File read and write
Define student class array , Yes N personal (N=5), Including the scores of the top three courses besides name and number of words , Implement file reading and writing of student array .

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(" Li 1 white ", 99, 99, 99);
		stu[1] = new Student(" Li 2 white ", 89, 89, 90);
		stu[2] = new Student(" Li 3 white ", 120, 120, 60);
		stu[3] = new Student(" Li 4 white ", 120, 120, 60);
		stu[4] = new Student(" Li 5 white ", 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(" Chinese language and literature :" + stu[i].Chinese);
				output.print(" mathematics :" + stu[i].English);
				output.println(" english :" + 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());
		}
	}
}
原网站

版权声明
本文为[Xiaomurong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221502448888.html