Hallo,
ich möchte gerne ein paar daten mit hilfe der java preference api speichern. Dazu benutze ich eine klasse von IBM:
Hier möchte ich nun zu programm beginn schauen ob es schon gespeicherte daten gibt:
Und wenn dem so ist sollen neue tabs im tabbed Pane erstellt werden:
Speichern tue ich die daten so:
Wenn ich zur laufzeit checke ob die daten bzw. der node gespeichert wurde ist alles ok, alle daten da. Wenn ich nun das programm schließe und neu starte sind alle daten futsch, woran kann das liegen?
ich möchte gerne ein paar daten mit hilfe der java preference api speichern. Dazu benutze ich eine klasse von IBM:
Java:
public class PrefObject
{
// Max byte count is 3/4 max string length (see Preferences
// documentation).
static private final int pieceLength =
((3*Preferences.MAX_VALUE_LENGTH)/4);
static private byte[] object2Bytes( Object o ) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( o );
return baos.toByteArray();
}
static private byte[][] breakIntoPieces( byte raw[] ) {
int numPieces = (raw.length + pieceLength - 1) / pieceLength;
byte pieces[][] = new byte[numPieces][];
for (int i=0; i<numPieces; ++i) {
int startByte = i * pieceLength;
int endByte = startByte + pieceLength;
if (endByte > raw.length) {
endByte = raw.length;
}
int length = endByte - startByte;
pieces[i] = new byte[length];
System.arraycopy( raw, startByte, pieces[i], 0, length );
}
return pieces;
}
static private void writePieces( Preferences prefs, String key,
byte pieces[][] ) throws BackingStoreException {
Preferences node = prefs.node( key );
node.clear();
for (int i=0; i<pieces.length; ++i) {
node.putByteArray( ""+i, pieces[i] );
}
}
static private byte[][] readPieces( Preferences prefs, String key )
throws BackingStoreException {
Preferences node = prefs.node( key );
String keys[] = node.keys();
int numPieces = keys.length;
byte pieces[][] = new byte[numPieces][];
for (int i=0; i<numPieces; ++i) {
pieces[i] = node.getByteArray( ""+i, null );
}
return pieces;
}
static private byte[] combinePieces( byte pieces[][] ) {
int length = 0;
for (int i=0; i<pieces.length; ++i) {
length += pieces[i].length;
}
byte raw[] = new byte[length];
int cursor = 0;
for (int i=0; i<pieces.length; ++i) {
System.arraycopy( pieces[i], 0, raw, cursor, pieces[i].length );
cursor += pieces[i].length;
}
return raw;
}
static private Object bytes2Object( byte raw[] )
throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream( raw );
ObjectInputStream ois = new ObjectInputStream( bais );
Object o = ois.readObject();
return o;
}
static public void putObject( Preferences prefs, String key, Object o )
throws IOException, BackingStoreException, ClassNotFoundException {
byte raw[] = object2Bytes( o );
byte pieces[][] = breakIntoPieces( raw );
writePieces( prefs, key, pieces );
}
static public Object getObject( Preferences prefs, String key )
throws IOException, BackingStoreException, ClassNotFoundException {
byte pieces[][] = readPieces( prefs, key );
byte raw[] = combinePieces( pieces );
Object o = bytes2Object( raw );
return o;
}
}
Hier möchte ich nun zu programm beginn schauen ob es schon gespeicherte daten gibt:
Java:
public MainUI() throws IOException, BackingStoreException, ClassNotFoundException
{
initComponents();
prefs = Preferences.userNodeForPackage(this.getClass());
newPref = new YPreferences();
System.out.println(this.getClass());
if(prefs.nodeExists("Settings") == true)
{
System.out.println("Create Tabbed Panes");
createTabbedPaneFromPreferences();
}
}
Und wenn dem so ist sollen neue tabs im tabbed Pane erstellt werden:
Java:
public void createTabbedPaneFromPreferences()
{
for(String name : newPref.stockNames)
{
System.out.println("FOR EACH");
StockPanelTemplate stockPanel = new StockPanelTemplate();
stockTabbedPanel.add(name, stockPanel);
}
}
Speichern tue ich die daten so:
Java:
if(prefs.nodeExists("Settings") == true)
{
System.out.println("GET EXISTING NODE");
newPref = (YPreferences)PrefObject.getObject(prefs, "Settings");
newPref.stockNames.add(name);
PrefObject.putObject(prefs, "Settings", newPref);
}
else
{
newPref.stockNames.add(name);
PrefObject.putObject(prefs, "Settings", newPref);
}
Wenn ich zur laufzeit checke ob die daten bzw. der node gespeichert wurde ist alles ok, alle daten da. Wenn ich nun das programm schließe und neu starte sind alle daten futsch, woran kann das liegen?