/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package util; import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; /** * * @author 1 */ public class saverestore { static RecordStore rs; public saverestore() { } public static boolean isSavedData(String name) { try { rs = RecordStore.openRecordStore(name, false); rs.closeRecordStore(); return true; } catch (RecordStoreException ex) { System.out.println("Record store " + name + " is not avaliable"); return false; } } public static void save(String name, int num, byte[] record) { try { rs = RecordStore.openRecordStore(name, true); if (rs.getNumRecords() == 0 || num > rs.getNumRecords()) { rs.addRecord(record, 0, record.length); } else { rs.setRecord(num, record, 0, record.length); } } catch (RecordStoreException ex) { } } public static void saveInt(String name, int num, int record) { String tmp = String.valueOf(record); byte[] b = tmp.getBytes(); try { rs = RecordStore.openRecordStore(name, true); if (rs.getNumRecords() == 0 || num > rs.getNumRecords()) { rs.addRecord(b, 0, b.length); } else { rs.setRecord(num, b, 0, b.length); } } catch (RecordStoreException ex) { } } public static void saveString(String name, int num, String record) { byte[] b = record.getBytes(); try { rs = RecordStore.openRecordStore(name, true); if (rs.getNumRecords() == 0 || num > rs.getNumRecords()) { rs.addRecord(b, 0, b.length); } else { rs.setRecord(num, b, 0, b.length); } } catch (RecordStoreException ex) { } } public static int restoreInt(String name, int num) { byte[] tmp = null; try { rs = RecordStore.openRecordStore(name, false); tmp = rs.getRecord(num); } catch (RecordStoreException ex) { } String s = new String(tmp); return Integer.parseInt(s); } public static String restoreString(String name, int num) { byte[] tmp = null; try { rs = RecordStore.openRecordStore(name, false); tmp = rs.getRecord(num); } catch (RecordStoreException ex) { } return new String(tmp); } public static byte[] restore(String name, int num) { byte[] tmp = null; try { rs = RecordStore.openRecordStore(name, false); tmp = rs.getRecord(num); } catch (RecordStoreException ex) { System.out.println("Record " + name + " #" + String.valueOf(num) + " is not avaliable!"); return null; } return tmp; } public static void close() { try { rs.closeRecordStore(); } catch (RecordStoreException ex) { } } }