简体   繁体   English

如何从RandomAccessFile中写入和删除对象?

[英]How do I write and delete Objects from a RandomAccessFile?

I am currently working on a school project for which I need to save my data to a RandomAccessFile . 我目前正在做一个学校项目,需要将我的数据保存到RandomAccessFile I figured that this is by far not the most efficient way, but I have to do it. 我认为这不是最有效的方法,但是我必须这样做。 Note: I have to use the RandomAccessFile class. 注意:我必须使用RandomAccessFile类。

I understand how I can save simple strings and int created in the main method to a file, but I am having trouble transferring this knowledge onto my own program. 我知道如何将简单的strings和在main方法中创建的int保存到文件中,但是我很难将这些知识转移到我自己的程序中。

I have 4 different classes, ie Database , Group , Student , Rehearsal . 我有4个不同的班级,即DatabaseGroupStudentRehearsal The Database class lets you add groups to a linked list of groups. Database类使您可以将组添加到组的链接列表中。 To each group, you can then add students (see below) as well as rehearsal dates (its a theatre management program). 然后,您可以向每个组添加学生(见下文)以及排练日期(其剧院管理程序)。 These are added to linkedlist<Student> and linkedlist<Rehearsal> respectively. 这些分别添加到linkedlist<Student>linkedlist<Rehearsal>

This is my addStudent method in the Group class that adds a student to the linkedlist of a group that was created. 这是我在Group类中的addStudent方法,可将学生添加到已创建的组的链表中。

public void addStudent(int id, String firstname, String lastname) throws IOException {
    Student newstudent = new Student(id, firstname, lastname);
    if (!Students.contains(newstudent)) {
        Students.add(newstudent);
} else 
        JOptionPane.showMessageDialog(null, "Student with ID " + id
                + " already exists in group!", "Error",
                JOptionPane.WARNING_MESSAGE);
}

How do I let the method automatically write the student object to the file when it is executed? 执行该方法时,如何让该方法自动将学生对象写入文件?

This is my removeStudent method: 这是我的removeStudent方法:

public void removeStudent(int id) {
    if (!Students.remove(new Student(id, null, null))) {
        JOptionPane.showMessageDialog(null, "Student with ID[" + id
                + "] not present. System unchanged.");

    } 
}

Pretty much the same question, how can I then delete a specific object from the file when the method is executed. 几乎是同样的问题,执行该方法后,如何从文件中删除特定对象。 If you could me help me out on that as well, that would be great :) 如果您能在这方面帮助我,那也很棒:)

override the toString() method of object class in the way you want to write to the file! 以您要写入文件的方式覆盖对象类的toString()方法!

then loop using foreach over the linkedlist and call toString() on student object and write to file 然后在linkedlist使用foreach循环并在学生对象上调用toString()并写入文件

example: 例:

fileOutputStream outStream=newFileOutputStream("../RandomAccessFile");//path where you want to create file
foreach(Student s in linkedlist)
{
  outStream.println(s.toString()+"/n");
}

You can't delete anything from a RandomAccessFile except by implementing some file protocol that your code understands, for example filling a record with nulls, assuming your file has records, and assuming that all-nulls (or whatever) is not a legal record value. 您不能从RandomAccessFile删除任何内容,除非实现代码可以理解的某种文件协议,例如,用空值填充记录,假设文件中包含记录,并假设全空(或其他值)不是合法的记录值。 Or implement a byte header on each record such that 1 means present and 0 means deleted, or vice versa. 或者在每个记录上实现一个字节标题,这样1表示存在,0表示删除, 反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM