[Tutorial] Using Properties for File Storage

Discussion in 'Resources' started by Kuuichi, Aug 14, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Kuuichi

    So I did a quick search and saw that no one has posted a proper tutorial on how to use the Properties class. (Here is some documentation on what I am referring to).

    Properties is a neat Java utility that allows for storing of keys/values (like a HashMap) in the form of strings. Now you may be wondering, why use Properties instead of the the Bukkit config or various other libraries that are much easier to use? Well, I've compiled a list!

    Why you should use Properties:
    • It can be used not only as a config, but as a means of storing data key/value style
    • It'll make your plugin more unique, being one that doesn't use the default means of config storage
    • It's a Java util, so you can use it not only for Bukkit plugins
    • You'll feel slightly accomplished afterwards
    What I use Properties for:
    • My configs
    • Storing data i.e. locations, custom player parameters (stats, custom levels), anything that needs to be stored and can be done in the form of a string
    So on to the tutorial...
    ---
    First of all, you'll want to create your Properties object.
    Code:java
    1. Properties props = new Properties();

    Then, you'll want both your Input and Output streams, as we'll be reading and writing from files.
    Code:java
    1. FileInputStream fis = new FileInputStream(your file directory);
    2. FileOutputStream out = new FileOutputStream(your file directory);

    Instead of constantly writing the directory, I like to create a constant for my File.
    Code:java
    1. // Of course, put this in your main bracket
    2. public static final File f = new File(your file directory);

    You don't have to create a File constant, but I like organization, so it's something I do.
    Don't forget, like any File handling you'll need to surround this code with try/catch, and make sure you check if the File exists first!
    Anyway, now you have to load the file into Properties with this method:
    Code:java
    1. props.load(fis);

    And now you can manipulate the file to your heart's desire!
    To create a key and pair it with a value:
    Code:java
    1. props.setProperty(String key, String value);

    To fetch a value from a key:
    Code:java
    1. props.getProperty("key");
    2.  
    3. // Returns a value

    And finally, when you're done, you should always dispose the streams and store the Properties file.
    Code:java
    1. props.store(out, "any comments should be written here, this will be in the header");
    2. fis.close();
    3. out.close();


    Some things to note:
    • Order of the properties do not matter
    • When only reading from a Properties file, you do not need a FileOutputStream as you are not storing anything. This means you also do not need the props.store() method to be called. Simply load the Properties file, and later close the InputStream
    • Properties can be used with any text file, IE txt, yml, props, you name it
    • When you setProperty of an already existing key, it will overwrite the old value
    • The keys are case-sensitive
    • Properties will not store objects for you (the put() method is misleading), you must first convert the Object to String, and fetch it again from the String
    Finally, an example:
    Code:java
    1. public void storePlayerLocation(Player p) {
    2. File file = new File("plugins/playerloc.props");
    3. try {
    4.  
    5. if (!file.exists()) {
    6. file.createNewFile();
    7. }
    8.  
    9. Properties props = new Properties();
    10. props.load(fis);
    11. props.setProperty(p.getName(), p.getLocation().toString());
    12. props.store(out, "Player Location Storage");
    13. out.close();
    14. fis.close();
    15.  
    16. } catch (IOException e) {
    17. e.printStackTrace();
    18. }
    19. }


    This method will store a player's location, which can later be fetched similarly. Obviously, this is a dumb way of storing location because a Player's location can be obtained via OfflinePlayer, but this is just an example.
    Thanks for reading!
     
  2. Offline

    Ultimate_n00b

    A few things:
    1. Location.toString() ? That doesn't work that well..
    2. What does the finished file look like? One reason Bukkit uses YAML is because they are easy to edit.
     
  3. Offline

    Kuuichi

    In the case that you DID want to store a location, the best way would be storing the world:x:y:z as a string, I just used that as an example. :p

    The finished file is exactly how you would imagine it, with (key=value) line by line. In fact, the server.properties file is handled with Properties. :D
     
    Ultimate_n00b likes this.
  4. Offline

    Ultimate_n00b

    After I typed that I realized that the server file is handled by Properties.

    I'll have to look into this. The only reason not to use this, is that Bukkit already has a YAML library included.
     
  5. Kuuichi Cool! Thanks for the tutorial!
     
  6. Offline

    moo3oo3oo3

    How would I get values from the properties? props.getPropery() does not work because it's not global.
     
  7. Offline

    Skionz

  8. Offline

    moo3oo3oo3

    I put all the code in a public void class and when I try to do props.getProperties() in one of my listener events, it can't find the variable
     
  9. Offline

    teej107

  10. Offline

    moo3oo3oo3

  11. Offline

    Skionz

  12. Offline

    teej107

  13. Offline

    moo3oo3oo3

    I'm not using a class. Should I?
     
  14. Offline

    Skionz

    moo3oo3oo3 Your not using a class? What are you using?
     
    teej107 likes this.
  15. Offline

    moo3oo3oo3

    Code:java
    1. public void storePlayercoins(Player player, int coins) {
    2. File file = new File("plugins/playerCoins.props");
    3. try {
    4.  
    5. if (!file.exists()) {
    6. file.createNewFile();
    7. }
    8.  
    9. Properties props = new Properties();
    10. props.load(in);
    11. props.setProperty(player.getUniqueId().toString(), String.valueOf(coins));
    12. props.store(out, "Player Coins");
    13. out.close();
    14. in.close();
    15.  
    16. } catch (IOException e) {
    17. e.printStackTrace();
    18. }
    19. }
     
  16. Offline

    teej107

  17. Offline

    Skionz

Thread Status:
Not open for further replies.

Share This Page