Flatfile-Configuration Reader Class

Discussion in 'Resources' started by narrowtux, Apr 18, 2011.

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

    narrowtux

    I made a Configuration reader class for flatfile formats if you don't like YAML or don't want to use a complex XML-Parser.

    An example for a flatfile configuration file:
    Code:
    #Next is a string
    Hello=I welcome you
    #this is an integer
    count=10
    #this is a boolean
    selfdestruct=false
    
    The FlatFileReader Class parses this type of configuration and provides a key-based access to the values.

    To read values from the file, create a new Instance of the FlatFileReader:
    Code:
    //FlatFileReader(File file, boolean caseSensitive)
    reader = new FlatFileReader(new File("path/to/config.cfg"), true);
    
    The second argument decides if the file should be read case-sensitive or not.

    After that, you can read all the values by these methods:
    • String getString(String key, String fallback)
    • int getInteger(String key, int fallback)
    • double getDouble(String key, double fallback)
    • float getFloat(String key, float fallback)
    • boolean getBoolean(String key, boolean fallback)
    As you can see, all the methods require the key and also a fallback. The fallback-value is returned when the key hasn't been found or the conversion from to the specific datatype has failed.

    For the example above, the file could be parsed like this:
    Code:
    String hello = reader.getString("Hello", "Hello World");
    int count = reader.getInteger("count", 2);
    boolean selfDestruct = reader.getBoolean("selfdestruct", false);
    
    You can get the source of the file here:
    View Source
    Download .java

    You can always ask me to add new datatypes, if they are generic enough. You are also free to modify the file if you want to.
     
  2. Offline

    matter123

    thank you this is just what i need for my plug in

    @narrowtux could you possibly add a way to save values to a file

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  3. Offline

    narrowtux

    Good idea, I can do that in some days after I come back from a vacation. This would remove all user-made comments, though.
    Also, I plan to implement the method Material getMaterial(String key, Material fallback);
     
  4. Offline

    matter123

    not a problem when my flat file should only be read by plugins
    ty
     
  5. Offline

    rymate1234

    @narrowtux Just 1 question (it seems good by the way)
    1. Does it generate the configuration file?
     
  6. Offline

    narrowtux

    Not at the moment ;)
     
  7. Its a cool reader class Thank you! Could you add the config writer? So if we change a value by command it can be written to the config file without changing other values?! :) Thank you anyways! :D
     
  8. Offline

    narrowtux

    I abandoned this thread after a while, because I put this (and more awesome stuff) into the NarrowtuxLib. You can find this class there (it also generates then).
     
  9. Offline

    Chromana

    Github 404'd. I guess this should be considered inactive.
     
Thread Status:
Not open for further replies.

Share This Page