用java创建一个学生类,从键盘上输入学生信息,可以把学生信息保存在指定文本文件中的指定位置;从文本文件中读取特定学生信息;在学生类中实现学生信息的增加、删除、修改、插入等操作;要求程序是带缓存的对文本文件进行读写操作
时间: 2024-02-06 17:12:02 浏览: 74
好的,我将依次回答你的问题。
首先,以下是一个基本的Java学生类的代码示例,用于保存学生信息:
```
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入学生性别:");
String gender = scanner.nextLine();
Student student = new Student(name, age, gender);
BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\student.txt"));
writer.write(student.getName() + "," + student.getAge() + "," + student.getGender());
writer.newLine();
writer.close();
}
}
```
在这个示例中,我们创建了一个学生类,其中包括学生的姓名、年龄和性别。我们使用Java的I/O API将学生信息保存到指定的文本文件中。
接下来,我们需要从文本文件中读取指定学生的信息。以下是实现该功能的代码示例:
```
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的学生姓名:");
String name = scanner.nextLine();
BufferedReader reader = new BufferedReader(new FileReader("D:\\student.txt"));
List<Student> students = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
Student student = new Student(fields[0], Integer.parseInt(fields[1]), fields[2]);
students.add(student);
}
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println("学生姓名:" + student.getName());
System.out.println("学生年龄:" + student.getAge());
System.out.println("学生性别:" + student.getGender());
}
}
reader.close();
}
}
```
在这个示例中,我们使用Java的I/O API从指定的文本文件中读取所有学生的信息,并将它们保存在一个ArrayList中。然后,我们遍历ArrayList以查找指定的学生信息。
现在,我们需要在学生类中实现学生信息的增加、删除、修改和插入操作。以下是一个示例代码,用于实现这些功能:
```
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择操作:");
System.out.println("1. 添加学生信息");
System.out.println("2. 删除学生信息");
System.out.println("3. 修改学生信息");
System.out.println("4. 插入学生信息");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
insertStudent();
break;
default:
System.out.println("无效的选择!");
break;
}
}
private static void addStudent() throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入学生性别:");
String gender = scanner.nextLine();
Student student = new Student(name, age, gender);
BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\student.txt", true));
writer.write(student.getName() + "," + student.getAge() + "," + student.getGender());
writer.newLine();
writer.close();
}
private static void deleteStudent() throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生姓名:");
String name = scanner.nextLine();
List<Student> students = readStudentsFromFile();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getName().equals(name)) {
students.remove(i);
}
}
writeStudentsToFile(students);
}
private static void modifyStudent() throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生姓名:");
String name = scanner.nextLine();
List<Student> students = readStudentsFromFile();
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println("请输入新的学生姓名:");
String newName = scanner.nextLine();
System.out.println("请输入新的学生年龄:");
int newAge = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入新的学生性别:");
String newGender = scanner.nextLine();
student = new Student(newName, newAge, newGender);
}
}
writeStudentsToFile(students);
}
private static void insertStudent() throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要插入的位置:");
int position = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入学生性别:");
String gender = scanner.nextLine();
Student student = new Student(name, age, gender);
List<Student> students = readStudentsFromFile();
students.add(position, student);
writeStudentsToFile(students);
}
private static List<Student> readStudentsFromFile() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("D:\\student.txt"));
List<Student> students = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
Student student = new Student(fields[0], Integer.parseInt(fields[1]), fields[2]);
students.add(student);
}
reader.close();
return students;
}
private static void writeStudentsToFile(List<Student> students) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\student.txt"));
for (Student student : students) {
writer.write(student.getName() + "," + student.getAge() + "," + student.getGender());
writer.newLine();
}
writer.close();
}
}
```
在这个示例中,我们使用Java的I/O API实现了学生信息的增加、删除、修改和插入操作。我们使用一个ArrayList来保存所有学生的信息,并将它们写入指定的文本文件。
最后,你提到了要求程序是带缓存的对文本文件进行读写操作。为了实现这个要求,我们可以使用Java的BufferedReader和BufferedWriter类来带缓存地读写文本文件。在我们的示例代码中,我们已经使用了这些类来读取和写入学生信息。因此,我们的程序已经满足了这个要求。
阅读全文