java - Saving/Loading Config Not Working -
first time posting here. heres problem: can't saving or loading of configuration file work. trying save player names based off how many players specified in code. know wrong , how fix? config file:
public void saveplayerconfiguration(string key, int value) { string path = "config.xml"; try { file file = new file(path); boolean exists = file.exists(); if (!exists) { file.createnewfile(); } outputstream write = new fileoutputstream(path); properties.setproperty(key, integer.tostring(value)); properties.storetoxml(write, key); } catch (exception e) { } } public void loadplayerconfiguration(string path) { try { inputstream read = new fileinputstream(path); properties.loadfromxml(read); string player1 = properties.getproperty("1"); string player2 = properties.getproperty("2"); string player3 = properties.getproperty("3"); string player4 = properties.getproperty("4"); string player5 = properties.getproperty("5"); string player6 = properties.getproperty("6"); read.close(); } catch (filenotfoundexception e) { saveplayerconfiguration("1", 1); saveplayerconfiguration("2", 2); saveplayerconfiguration("3", 3); saveplayerconfiguration("4", 4); saveplayerconfiguration("5", 5); saveplayerconfiguration("6", 6); loadconfiguration(path); } catch (ioexception e) { } }
options file:
private int width = main.width; private int height = main.height; private string player1 = "player1", player2 = "player2", player3 = "player3", player4 = "player4", player5 = "player5", player6 = "player6"; private string[] playernames = { player1, player2, player3, player4, player5, player6 }; private int[] player = { 1, 2, 3, 4, 5, 6 }; private int playertotal; private jbutton ok; private jtextfield input1, input2, input3, input4, input5, input6; private jtextfield[] playernameinput = { input1, input2, input3, input4, input5, input6 }; private jlabel playername; private rectangle rok, rplayeramount; private choice playeramount = new choice(); configuration config = new configuration(); private int button_width = 80; private int button_height = 40; jpanel window = new jpanel(); public database() { settitle("database - excelteor launcher"); setsize(new dimension(width, height)); add(window); setlocationrelativeto(null); setresizable(false); setvisible(true); window.setlayout(null); drop(); drawbuttons(); window.repaint(); } private void drawbuttons() { ok = new jbutton("ok"); rok = new rectangle((width - 100), (height - 70), button_width, button_height); ok.setbounds(rok); window.add(ok); rplayeramount = new rectangle(30, 130, 80, 25); playeramount.setbounds(rplayeramount); playeramount.add("1"); playeramount.add("2"); playeramount.add("3"); playeramount.add("4"); playeramount.add("5"); playeramount.add("6"); playeramount.select(1); window.add(playeramount); playername = new jlabel("player names:"); playername.setbounds(30, 110, 120, 20); window.add(playername); (int = 0; < playertotal; i++) { playernameinput[i] = new jtextfield(); playernameinput[i].setbounds(80, 150 + * 20, 60, 20); window.add(playernameinput[i]); } ok.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { dispose(); system.out.println("test"); (int = 0; < playertotal; i++) { config.saveplayerconfiguration(parseplayers(), player[i]); system.out.println(player[i]); } } }); } private void drop() { int selection = playeramount.getselectedindex(); if (selection == 0) { playertotal = 1; } if (selection == 1 || selection == -1) { playertotal = 2; } if (selection == 2) { playertotal = 3; } if (selection == 3) { playertotal = 4; } if (selection == 4) { playertotal = 5; } if (selection == 5) { playertotal = 6; } } private string parseplayers() { try { (int = 0; < playertotal; i++) { playernames[i] = playernameinput[i].tostring(); return playernames[i]; } } catch (numberformatexception e) { drop(); return player1; } return player1; }
my guess path of config.xml, 1 being passed loadplayerconfiguration(string path), wrong. try out below options resolving problem.
- make sure 'path' correct.
- make sure 'path' exists using exists() method of file api.
- if path relative, confirm whether path structure has been added onto class path. else, try placing in root of project.
if still face issues after trying out these steps, kindly share project structure.
cheers, madhu.
** edit ** @user2399785 : got clear idea on problem after going through config.xml.
<properties> <comment>javax.swing.jtextfield[,80,150,60x20,invalid,layout=javax.swing.plaf.basic.basictextui$updatehandler,alignmentx=0.0,alignmenty=0.0,border=javax.swing.plaf.borderuiresource$compoundborderuiresource@69ecade2,flags=296,maximumsize=,minimumsize=,preferredsize=,caretcolor=sun.swing.printcoloruiresource[r=51,g=51,b=51],disabledtextcolor=javax.swing.plaf.coloruiresource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.insetsuiresource[top=0,left=0,bottom=0,right=0],selectedtextcolor=sun.swing.printcoloruiresource[r=51,g=51,b=51],selectioncolor=javax.swing.plaf.coloruiresource[r=184,g=207,b=229],columns=0,columnwidth=0,command=,horizontalalignment=leading]</comment> <entry key="javax.swing.jtextfield[,80,150,60x20,invalid,layout=javax.swing.plaf.basic.basictextui$updatehandler,alignmentx=0.0,alignmenty=0.0,border=javax.swing.plaf.borderuiresource$compoundborderuiresource@69ecade2,flags=296,maximumsize=,minimumsize=,preferredsize=,caretcolor=sun.swing.printcoloruiresource[r=51,g=51,b=51],disabledtextcolor=javax.swing.plaf.coloruiresource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.insetsuiresource[top=0,left=0,bottom=0,right=0],selectedtextcolor=sun.swing.printcoloruiresource[r=51,g=51,b=51],selectioncolor=javax.swing.plaf.coloruiresource[r=184,g=207,b=229],columns=0,columnwidth=0,command=,horizontalalignment=leading]">2</entry> <entry key="javax.swing.jtextfield[,80,150,60x20,invalid,layout=javax.swing.plaf.basic.basictextui$updatehandler,alignmentx=0.0,alignmenty=0.0,border=javax.swing.plaf.borderuiresource$compoundborderuiresource@2904a7cf,flags=296,maximumsize=,minimumsize=,preferredsize=,caretcolor=sun.swing.printcoloruiresource[r=51,g=51,b=51],disabledtextcolor=javax.swing.plaf.coloruiresource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.insetsuiresource[top=0,left=0,bottom=0,right=0],selectedtextcolor=sun.swing.printcoloruiresource[r=51,g=51,b=51],selectioncolor=javax.swing.plaf.coloruiresource[r=184,g=207,b=229],columns=0,columnwidth=0,command=,horizontalalignment=leading]">2</entry> </properties>
if notice .xml, key contains kind of long javax swing detail. loadplayerconfiguration(string path) uses properties.getproperty("2")
return value key "2" , not vice versa. since there no key "2", getting return value null. try same code key in .xml , should fine.
cheers, madhu.
Comments
Post a Comment