1

我已经使用此代码将 Object 存储到文件中:

try{  
      FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");


      ObjectOutputStream save = new ObjectOutputStream(saveFile);

      save.writeObject(x);

      save.close();
    }
    catch(Exception exc){
      exc.printStackTrace();
    }
  }
}

如何删除单个对象?怎么清除文件??

4

3 回答 3

2

Ernest 是对的,因为从对象流中删除特定对象稍微复杂一些。他也是对的,当你想清空一个文件时,你可以简单地打开它进行写入并关闭它。但是,如果您想从文件系统中删除它,可以使用File对象来完成(不要忘记处理异常并正确返回值)。以下示例可能并不完美,但它应该为您提供有关如何使用纯 Java 实现目标的提示。希望这可以帮助...


package test;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


public class Main {

  public static void main(String[] args) throws Exception {
    String filename = "object.serialized";
    {
      List objects = new ArrayList();
      objects.add("String1");
      objects.add("String2");
      objects.add("String3");
      writeObjectsToFile(filename, objects);
    }

    {
      List objects = readObjectsFromFile(filename);
      objects.remove(1);
      writeObjectsToFile(filename, objects);
    }

    {
      List objects = readObjectsFromFile(filename);
      for (Object object : objects) {
        System.out.println(object);
      }
    }

    emptyFile(filename);
    deleteFile(filename);
  }

  private static void emptyFile(String filename) throws IOException {
    OutputStream os = null;
    try {
      os = new FileOutputStream(filename);
    } finally {
      if (os != null) {
        os.close();
      }
    }
  }

  private static void deleteFile(String filename) {
    File f = new File(filename);
    if (f.delete()) {
      System.out.println(filename + " deleted sucessfully...");
    } else {
      System.out.println(filename + " deletion failed!");
    }
  }

  private static void writeObjectsToFile(String filename, List objects) throws IOException {
    OutputStream os = null;
    try {
      os = new FileOutputStream(filename);
      ObjectOutputStream oos = new ObjectOutputStream(os);
      for (Object object : objects) {
        oos.writeObject(object);
      }
      oos.flush();
    } finally {
      if (os != null) {
        os.close();
      }
    }
  }

  private static List readObjectsFromFile(String filename) throws IOException, ClassNotFoundException {
    List objects = new ArrayList();
    InputStream is = null;
    try {
      is = new FileInputStream(filename);
      ObjectInputStream ois = new ObjectInputStream(is);
      while (true) {
        try {
          Object object = ois.readObject();
          objects.add(object);
        } catch (EOFException ex) {
          break;
        }
      }
    } finally {
      if (is != null) {
        is.close();
      }
    }
    return objects;
  }

}

输出:


String1
String3
object.serialized deleted sucessfully...

于 2012-04-07T15:04:47.157 回答
2

好吧,清空一个文件很容易——只需打开它进行写入,然后再次关闭它:

new FileOutputStream("SaveObj.sav").close();

那会清空它。但是,如果您试图从许多对象中删除一个对象,那就要复杂得多。您要么必须读入所有对象并只写出要保留的对象,要么必须保留每个对象开始的文件偏移量的索引(可能在单独的文件中)。点您要考虑改用对象数据库。

于 2012-04-07T14:39:41.337 回答
0

我知道这个主题已经很久了,但为了帮助未来的人,对我有用的是再次将对象写为空值:

public static void writeIncidentsObjectsInCache(Object object) throws IOException {
    writeObject(INCIDENTS_CACHE, object); }

public static Object readIncidentsObjectFromCache() throws IOException,
        ClassNotFoundException {
    return readObject(INCIDENTS_CACHE); }

public static void clearIncidents() throws IOException, ClassNotFoundException {
    writeIncidentsObjectsInCache(null); }

public static void writeObject(String key, Object object) throws IOException {
    FileOutputStream fos = TheAAApp.getApp().openFileOutput(key, Context.MODE_PRIVATE);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(object);
    oos.close();
    fos.close();
}

public static Object readObject(String key) throws IOException,
        ClassNotFoundException {
    FileInputStream fis = TheAAApp.getApp().openFileInput(key);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object object = ois.readObject();
    return object;
}
于 2016-05-19T15:59:52.500 回答