0

私は現在、人に関するデータを.datファイルに保存するプログラムを作成しようとしています。プログラムは、エントリの追加、エントリの表示、およびエントリの削除ができる必要があります(これらはすべて同じタイプのオブジェクトです)。

私が遭遇している問題は、エントリの削除にあります。ランダムアクセスファイルを使用しないように特に指示されましたが、データの特定の場所を削除できる必要があります。ポインタの種類を制御する方法がないため、削除するエントリを定義する方法がわかりません。エントリの場所を見つける方法はありますが、オブジェクトの場所にあるデータを削除する方法がありません。

private static Person findPerson(String theName)
{
    Person thePerson = null;

    try
    {
        inputStream = new ObjectInputStream(new FileInputStream(personFile));

        while(!done) //While the name has not been found
        {
            thePerson = (Person)inputStream.readObject();

            if(theName.equals(thePerson.getName())) //If the name is found
            {
                done = true;
            }
        }
        done = false;

        inputStream.close(); //Close the stream
    }

    catch(IOException e) //If the end of the file is reached
    {
        System.out.println("The name you specified is not in the file.");
    }
    catch(ClassNotFoundException e) //If there is trouble
    {
        System.out.println("Problems with file input.");
    }

    return thePerson; //Return the record
}

ObjectOutputStreamがデータを削除する場所を定義する方法はありますか?特定のエントリを削除するにはどうすればよいですか?

4

1 に答える 1

1

いつでも新しい一時ファイルを作成し、whileループで、関心のあるエントリのみを追加できます。古いファイルを削除して一時ファイル名を変更した後

于 2011-03-12T22:58:02.953 に答える