如何从随机访问文件中删除记录?
我想知道如何从随机访问文件中删除记录。
以下是我添加到 RAF 的方法,但不知道如何删除它:X
public void addNewStudent(String name, String formClass, String emailAddress, String country1, String country2, String universityChoice)
{
try
{
RandomAccessFile theFile = new RandomAccessFile("studentData.dat","rw");
long records = (theFile.length()+299)/300; //Number of records
if(theFile.length()>0) //Check if the file is empty or not
{
for(long x=0;x<records;x++)
{
theFile.seek(x*300);
String currentName = theFile.readUTF();
if(name.equalsIgnoreCase(currentName)) //Check if student exists in database
{
output("This student exist already"); //Output this if exists
}
else // or else write a new record
{
theFile.seek(records*300);
theFile.writeUTF(name); //Write student name
theFile.seek((records*300)+50);
theFile.writeUTF(formClass); //Writes students' form class
theFile.seek((records*300)+60);
theFile.writeUTF(emailAddress); //Writes students' email
theFile.seek((records*300)+100);
theFile.writeUTF(country1); //Writes students' country choice #1
theFile.seek((records*300)+140);
theFile.writeUTF(country2); //Writes students' country choice #2
theFile.seek((records*300)+180);
theFile.writeUTF(universityChoice); //Writes students' university choices
students.add(name,formClass,emailAddress,country1,country2,universityChoice);
}
}
}
else //If the file isn't empty, then just write
{
theFile.seek(records*300);
theFile.writeUTF(name); //Write student name
theFile.seek((records*300)+50);
theFile.writeUTF(formClass); //Writes students' form class
theFile.seek((records*300)+60);
theFile.writeUTF(emailAddress); //Writes students' email
theFile.seek((records*300)+100);
theFile.writeUTF(country1); //Writes students' country choice #1
theFile.seek((records*300)+140);
theFile.writeUTF(country2); //Writes students' country choice #2
theFile.seek((records*300)+180);
theFile.writeUTF(universityChoice); //Writes students' university choices
students.add(name,formClass,emailAddress,country1,country2,universityChoice);
}
}
catch(IOException e)
{
output("Error while adding new student");
}
}
I was wondering how I could delete a record from my random access file.
Here's how I add to my RAF, not sure how to delete it though :X
public void addNewStudent(String name, String formClass, String emailAddress, String country1, String country2, String universityChoice)
{
try
{
RandomAccessFile theFile = new RandomAccessFile("studentData.dat","rw");
long records = (theFile.length()+299)/300; //Number of records
if(theFile.length()>0) //Check if the file is empty or not
{
for(long x=0;x<records;x++)
{
theFile.seek(x*300);
String currentName = theFile.readUTF();
if(name.equalsIgnoreCase(currentName)) //Check if student exists in database
{
output("This student exist already"); //Output this if exists
}
else // or else write a new record
{
theFile.seek(records*300);
theFile.writeUTF(name); //Write student name
theFile.seek((records*300)+50);
theFile.writeUTF(formClass); //Writes students' form class
theFile.seek((records*300)+60);
theFile.writeUTF(emailAddress); //Writes students' email
theFile.seek((records*300)+100);
theFile.writeUTF(country1); //Writes students' country choice #1
theFile.seek((records*300)+140);
theFile.writeUTF(country2); //Writes students' country choice #2
theFile.seek((records*300)+180);
theFile.writeUTF(universityChoice); //Writes students' university choices
students.add(name,formClass,emailAddress,country1,country2,universityChoice);
}
}
}
else //If the file isn't empty, then just write
{
theFile.seek(records*300);
theFile.writeUTF(name); //Write student name
theFile.seek((records*300)+50);
theFile.writeUTF(formClass); //Writes students' form class
theFile.seek((records*300)+60);
theFile.writeUTF(emailAddress); //Writes students' email
theFile.seek((records*300)+100);
theFile.writeUTF(country1); //Writes students' country choice #1
theFile.seek((records*300)+140);
theFile.writeUTF(country2); //Writes students' country choice #2
theFile.seek((records*300)+180);
theFile.writeUTF(universityChoice); //Writes students' university choices
students.add(name,formClass,emailAddress,country1,country2,universityChoice);
}
}
catch(IOException e)
{
output("Error while adding new student");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会有一个已删除的标志,如果已设置,请不要读入它。
I would have a deleted flag and if its set, don't read it in.
最好的方法:创建一个删除对话框方法。让该方法将空白记录写入文件。如果您的构造函数设置正确,那么创建空白记录很容易。
当空白记录被写入一系列零和空值时,如果您的程序设置为仅显示具有正确值的记录,则它将不会显示任何具有 0/空值的记录。
Best way: create a delete dialog method. Have that method write a blank record to the file. Creating a blank record is easy if your constructor(s) are set up correctly.
When the blank record is written as a series of zeroes and nulls, and if your program is set to only show records with proper values, it won't show any records with 0/null values.