0

I've added storage file to my codenameone application. In some event I wanna delete specific storage file and recreate it with some old filtered data and new data. It doesn't work well on deletion.

First I create method for clear storage file in StorageManager class:

public void clearData(String pStorageTable){
    Storage.getInstance().deleteStorageFile(pStorageTable);
}

In other class I use this method like this:

// load all data of specific storage file
// ...

new DBManager().clearData(ThreeTrans.DB_NAME);

// write old data with filtering of specific ID and new data
// ...

here is method of write data:

public void write(ThreeTrans pTT){
    if(store == null) {
        store = Storage.getInstance();
    }

    DB_NAME = "TT";

    if(!store.exists(DB_NAME)) {
        Hashtable depHash = new Hashtable();
        String k = "1" + pTT.getNumber();
        depHash.put(k, pTT.toString());
        store.writeObject(DB_NAME, depHash);
    }
    else {
        Hashtable depHash = (Hashtable)store.readObject(DB_NAME);

        if (!depHash.containsValue(pTT.getNumber())) {
            String k = String.valueOf(getLastKeyNumber());
            depHash.put(k, pTT.toString());
            store.writeObject(DB_NAME, depHash);
        }
    }
}

at first I was using this method for delete storage file:

public void clearData(String pStorageTable){
    if(store == null) {
        store = Storage.getInstance();
    }

    for (String str : store.listEntries()) {
        if(str.toLowerCase().startsWith(pStorageTable)) {
           store.deleteStorageFile(str);
        }
    }
}

and after this problem this method changed to this;

public void clearData(String pStorageTable){
    Storage.getInstance().deleteStorageFile(pStorageTable);
}
Ahmad
  • 437
  • 5
  • 20

1 Answers1

0

I'm assuming that you didn't invoke close() on the output stream or the input stream working with the file and there is still a lock on the file. This is usually the case for such issues.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • I use writeObject method for saving data to storage file and readObject for read data from storage file. Is it need to invoke close() method? – Ahmad May 22 '13 at 03:05
  • No. Can you post the code including the write code and the proper delete code? Should work pretty seamlessly. Is this on the simulator or device or both? – Shai Almog May 22 '13 at 06:41
  • I edited my question. Answer of your second question is YES, I have different output for this problem and both of them (Simulator and device) are not correct. – Ahmad May 22 '13 at 08:54
  • 1
    It looks like you initialize DB_NAME at write so in the next app invocation it won't be initialized. – Shai Almog May 22 '13 at 15:15
  • I did it another way to solve it (Just by replacing data). Thanks Shai – Ahmad May 26 '13 at 04:57