[HELP] How to read Strings from the Resource file?

Discussion in 'Plugin Development' started by Steffion, Oct 27, 2012.

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

    Steffion

    Hello everyone who is ready this,

    I really need help with my plugin. I searched everywhere and I couldn't find it.
    As the title says How can I read a String (or Boolean) from the Resource File? Is this possible? If true, how?

    I hope you understand this. Please help me out!
    Greetings,
    Steffion!
     
  2. Offline

    skipperguy12

    What Resource file? Are you talking about a string in a config your plugin generated? If so, take a look at this:
    http://wiki.bukkit.org/Configuration_API_Reference

    From their examples:
    Setting a string:
    Code:
    // setting a String
    String stringValue = "Hello World!";
    this.getConfig().set("path.to.string", stringValue);
    And to get a string:
    Code:
    //Getting a string
    this.getConfig.getString(String);
     
  3. Offline

    Vandrake

    Code:
    File pladata = new File(getDataFolder()+ File.separator + "filename.yml");
    FileConfiguration plset = YamlConfiguration.loadConfiguration(pladata);
    plset.getInt(for integers)
    plset.getBoolean(for booleans)
    plset.getString(for strings)
    Just type plset. and wait
     
  4. Offline

    Steffion

    skipperguy12
    Hello Skipper, Thanks for replying.

    I don't mean the generated one but the one you see in the "src/" folder.

    Vandrake
    Is this the Generated file or the one from the "src/" folder?

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

    skipperguy12

    Just looked through my server files, can't seem to find a scr/ folder.

    Where would it be?
     
  6. Offline

    Steffion

    skipperguy12
    Its the folder where the plugin is written in. (So inside the jar.)
    I think its impossible to read it but if it could please help!
     
  7. Offline

    skipperguy12

    OHH, you mean in eclipse! I'm pretty sure thats impossible, because its a jar. Either ways, if someone DOES figure out how to do it, you would still get an error if you try to alter anything: Java SE Binary is already in use or something like that. :L
     
  8. Offline

    Steffion

  9. How about
    Code:
    new BufferedReader(new InputStreamReder(<yourmainclass>.class.getResourceAsStream("file.txt")));
    then you can use the methods from the BufferedReader to get things from your file, it's that easy.
     
  10. Offline

    Steffion

    kumpelblase2
    And how to get the string then ;#?

    kumpelblase2 Because I want to get a specific config out of the resource and put it in a String. I hope you understand me ;#!

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

    one4me

    Code:
    /*Method to read lines*/
    public ArrayList<String> getLines(String resource) {
      ArrayList<String> lines = new ArrayList<String>();
      Scanner s = new Scanner(getClass().getResourceAsStream(resource));
      while(s.hasNextLine()) {
        lines.add(s.nextLine());
      }
      s.close();
      return lines;
    }
     
    /*Getting lines from specified resource*/
    ArrayList<String> lines = getLines(/*resourceName*/);
     
    /*Getting string from specified line number*/
    String line = lines.get(/*lineNumber*/);
    
    I do prefer using Scanner over BufferedReader because I find it easier to use, however to make it even easier you get the line from the list Resources.readLines(URL url, Charset charset) returns.

    Still, I'm not sure why you'd want to store a variable like this.

    Edit - I forgot to close the Scanner in the method.
     
Thread Status:
Not open for further replies.

Share This Page