简体   繁体   中英

java.util.Properties not working

I'm working on a game and I'm using java.util.Properties to save the dates. I want to save this dates in a XML file but I'm having some problems...

This is the method which creates the OutputStream, the XML file and the directory if not exist.

public String save(String date, String vname){
    //This is the method which saves the dates

   //There are some controls
    if(!dir.exists()){
        game.log.debug("Saves directory not exists. Creating...", -3);
        if(!dir.mkdirs())
            game.log.debug("Impossible to create directory. The game can't save dates.", -1);
        else game.log.debug("\"C:/MakeGame/saves\" successfully created.", 1);
    }

    if(ostream == null && save == null){
        try {
            if(save == null){
                save = new File(dir.getPath() + game.getName() + " save.xml"); //THIS CREATES THE SAVE FILE IN XML FORMAT
                ostream = new FileOutputStream(save); //THIS CREATE A NEW FILEOUTPUTSTREAM
            }
            if(!save.exists()){
                if(!save.createNewFile()) //NEW XML FILE CREATION
                    game.log.debug("Impossible to make new save file. The game can't save dates.", -1);
                else game.log.debug("\"C:/MakeGame/saves/" + save.getName() + " \" successfully created.", 1);
            }
        } catch (FileNotFoundException ex) {
            game.log.debug("File not found. The game can't save dates.", -1);
        } catch (IOException ex) {
            Logger.getLogger(Saver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    String saved = "";

    if(save.exists()){
        saved = (String) saves.setProperty(date, vname); //SET THE PROPERTY

        try {
            saves.storeToXML(ostream, "Don't change this save files. This files are used by the games to load the progresses."); //STORES THE DATES IN XML FORMAT.
        } catch (IOException ex) {
            game.log.debug("Impossible to store this saves. The game can't save dates", -1);
        }
    }

    return saved;
}

When I load the dates I use another method.

public String load(String vname) throws FileNotFoundException{
        if(save == null){
            save = new File(dir.getPath() + game + " save.save");
            istream = new FileInputStream(save);
        }

        if(save.exists()) try {
            saves.load(istream);
        } catch (IOException ex) {
            game.log.debug("Error loading dates from save file. The game can't load dates.", -1);
        }
        if(!saves.isEmpty()){
            game.log.debug(saves.getProperty(vname) + " successfully loaded.", 0);
            return saves.getProperty(vname);
        }
        return null;
    }

But it returns always null. Why?

You are using your date string as the property key in your save() method, and then attempting to use vname as the key in your load() method.

Use vname as your key and date as your value when you are setting the property like this:

saved = (String) saves.setProperty(vname, date); //SET THE PROPERTY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM