Read Server.Properties help

Discussion in 'Plugin Development' started by LRFLEW, Jan 21, 2011.

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

    LRFLEW

    I don't have a ton of experience with java, and no experience what-so-ever in any language about accessing files. How do I read Server.Properties and look for a line that starts with, say, "awesome-file="? How do I read files in general, really :p (I'm just focusing on the server properties file for now). When I access it, do I need to call the file path from the plugins folder or the folder where craftbukkit.jar resides?

    N00B questions FTL.

    LRFLEW
     
  2. Offline

    Novar234

  3. Offline

    Mixcoatl

    Reading a properties file in simple in Java:
    Code:
    FileInputStream in = null;
    try {
        // Initially empty.
        Properties properties = new Properties();
    
        // You can read files using FileInputStream or FileReader.
        in = new FileInputStream(filename);
    
        // This line reads the properties file.
        properties.load(in);
    
        // Your code goes here.
    } catch (FileNotFoundException ex) {
        // Handle missing properties file errors.
    } catch (IOException ex) {
        // Handle IO errors.
    } finally {
        // Need to do some work to close the stream.
        try {
            if (in != null) in.close();
        } catch (IOException ex) {
            // Handle IO errors or log a warning.
        }
    }
     
  4. Offline

    LRFLEW

    Thanks this is really helpful. I do have some questions though.
    Code:
    Properties properties = new Properties();
    properties.load(in);
    What does this actually do :p
    Code:
    catch (IOException ex)
    when is IOException called?
    Code:
    new FileInputStream(filename)
    is the filename call from the plugin folder or the server folder?
     
  5. Offline

    1337

    the IOexception is what happens if there is an error normally the file does not exist filename is the name of the file and the properties load loads the prop file
     
  6. Offline

    LRFLEW

    Have you ever heard of punctuation?

    Form what I can get from it is:
    IOexception is called when there is an error reading the file, but exists (such as if read privileges are denied).
    you didn't answer where the file is called from (or where the read file needs to be)
    and "the properties loads the prop file" can't get any less specific.
     
  7. Offline

    Mixcoatl

    The first line constructs an instance of the Properties class. This class is provided by the JDK specifically for reading, writing, and accessing properties files.
    The second line reads the contents of the specified stream and populates its contents into the properties object.
    As 1337 wrote, an IOException is thrown when there is a problem reading or writing a file. Generally the problem is not of the missing file sort (that case usually throws FileNotFoundException, specifically). It may be a permission error or the file may not contain the kind of information expected (for example: instructing a properties object to load a binary file).
    The filename is relative to the current directory where the application is running. This should be your top-level Bukkit server directory.
     
  8. Offline

    matjam

    Creates a new instance of the Properties class called 'properties', then calls the load method on 'properties' to load the properties from the FileInputStream 'in'.

    If there is an IOException. Exceptions are errors, raised by a class (in this case, Java's standard library) when there is an error. If you look at the Properties.load() method, you can see that it says that it will throw

    Just doing a catch (SomeException e) and then doing nothing with it is bad practice though, one should at least emit an error message.

    Exceptions are caught by the catch block at the moment that they are raised so the latter parts of the code can be skipped and your error handling code can be all in the one place. This is better than checking the return code on every method call ...

    Bukkit doesn't change the current working directory, so its whatever directory you were in when you started bukkit.

    edit: lol, typical, write a detailed response, someone else answers in the meantime :p
     
Thread Status:
Not open for further replies.

Share This Page