2012-04-07 87 views
1

我已經使用這個代碼,對象存儲到文件:如何刪除存儲在Java的FileOutputStream中的單個對象

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


     ObjectOutputStream save = new ObjectOutputStream(saveFile); 

     save.writeObject(x); 

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

如何刪除單個對象? 如何清除文件?

回答

1

嘛,清空出文件是非常容易 - 只需打開它進行寫作,並再次將其關閉:

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

這會空出來。但是,如果你試圖從多個對象中刪除一個對象,那就更加複雜了。你必須讀入所有的對象,只寫出你想保留的對象,或者你必須保存每個對象開始的文件偏移索引(可能在一個單獨的文件中)。您可以考慮使用對象數據庫。

1

歐內斯特是對的,因爲從對象流中移除特定的對象稍微複雜一些。他也是對的,當你想清空一個文件時,你可以簡單地打開它來寫入並關閉它。但是如果你想從文件系統中刪除它,可以使用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... 
 
+0

感謝您的幫助 – AndreaF 2012-04-07 19:19:45

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; 
}