允许用户从Java的二进制文件中编辑学生信息



我正在做一个学校项目。它基本上允许用户使用scanner创建一个学生,为学生分配和id,然后将数据写入一个名为studentsinfo.dat的二进制文件。创建学生后,用户可以显示二进制文件中的信息,也可以编辑学生。我把展示部分做好了。然而,我很难允许他们编辑,程序要求用户输入要编辑的学生id,程序应该检查文件中是否存在用户输入,但我在移动文件指针以查找id并将其与用户输入进行比较方面遇到了问题,我尝试了一些方法,但没有成功。任何想法都将不胜感激,这是我的代码:

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.*;
public class MidTermProject {

static AtomicInteger idGenerator = new AtomicInteger(0001);
static int id;

public static int getId() {
return id;
}
///Create Student method
public static void CreateStudent() throws IOException {
String FullName;
String address;
String city;
String state;
String newStudentID;

Scanner keyboard = new Scanner(System.in);

FileOutputStream fstream =
new FileOutputStream("StudentInfo.dat", true);
DataOutputStream outputFile =
new DataOutputStream(fstream);

System.out.print("nPlease enter your information bellow.n" + "nFull Name: ");
FullName = keyboard.nextLine();
outputFile.writeUTF(FullName);

System.out.print("Address: ");
address = keyboard.nextLine();
outputFile.writeUTF(address);

System.out.print("City: ");
city = keyboard.nextLine();
outputFile.writeUTF(city);

System.out.print("State: ");
state = keyboard.nextLine();
outputFile.writeUTF(state);

id = idGenerator.getAndIncrement();
String student = Integer.toString(getId());
outputFile.writeUTF(student);

outputFile.close();

System.out.print("nDonen");

}
///Edit Student method
public static void EditStudent() throws IOException {
String editName;
String editaddress;
String editCity;
String editState;
int editID;

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter the ID of the student you would like to edit: ");
editID = keyboard.nextInt();

RandomAccessFile file = 
new RandomAccessFile("StudentInfo.dat", "rw");
file.seek(10);

char ch = file.readChar();

if(editID == ch) {
System.out.print("nPlease enter NEW information bellow.n" + "nFull Name: ");
editName = keyboard.nextLine();
file.writeUTF(editName);

System.out.print("Address: ");
editaddress = keyboard.nextLine();
file.writeUTF(editaddress);

System.out.print("City: ");
editCity = keyboard.nextLine();
file.writeUTF(editCity);

System.out.print("State: ");
editState = keyboard.nextLine();
file.writeUTF(editState);

file.close();

System.out.print("Successfully Edited");
} else {
System.out.print("error");
}
}
///Display Student method
public static void DisplayStudent() throws IOException {
FileInputStream fstream = new FileInputStream("StudentInfo.dat");
DataInputStream inputFile = new DataInputStream(fstream);

String student;
boolean endOfFile = false;

while(!endOfFile)
{
try
{
student = inputFile.readUTF();
System.out.print(student + "n");
}
catch (EOFException e)
{
endOfFile = true;
}
}
System.out.println("nDone");

inputFile.close();
}

据我所知,你试图逐行输入学生的记录,所以当你编辑时,你会寻找那一行并在那里编辑,对吗?如果是这样的话,您至少需要在存储记录时将其分开,例如在行的末尾添加一个,然后在新行上写入下一个记录,然后您应该能够根据id更新某行上的记录。您可以实现Student类的toString((方法,只需向文件student1.toString(。

此外,您正在将学生数据字符串一个接一个地写入,因此,您将获得JohnDoeAddressCity,然后很难将其解析回学生实例。考虑例如用逗号-";姓名、姓氏、地址、城市";。