[SOLVED]Check for definition method if a config

Discussion in 'Plugin Development' started by xGhOsTkiLLeRx, Dec 19, 2011.

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

    xGhOsTkiLLeRx

    Hey there!

    I need some help. I have to check out, how the config is "stored" (set)

    old format
    Code:
    name=COLOR
    new format
    Code:
    name: COLOR
    Is there any way, how I could check, whether there is a "=" or a ": "?
    I already thought about if contains...., then replace the = part.

    Any ideas?

    Greetings
     
  2. You could use a FileReader and do something like:
    Code:java
    1.  
    2. while (reader.hasNext()) {
    3. String s = (String) reader.getNext(); // Don't remember if it needs cast
    4. s.replace("=", ": ");
    5. } finally {
    6. reader.flush();
    7. reader.close();
    8. }
    9.  

    That should work as is, if not you get the point
     
    xGhOsTkiLLeRx likes this.
  3. Offline

    xGhOsTkiLLeRx

    @tips48

    I use this method (working for me :p), 'cause your's doesn't write to the file again. It changes the String, but nothing more :D

    Code:java
    1.  
    2. public void updateConfig(File colors) throws Exception {
    3. BufferedReader reader = new BufferedReader(new FileReader(colors));
    4. // Create a file called temp.txt
    5. File tempFile = new File(getDataFolder(), "temp.txt");
    6. BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
    7. String line;
    8. try {
    9. while ((line = reader.readLine()) != null) {
    10. // if the old system is found update
    11. if (line.contains("=")) {
    12. String newLine = line.replace("=", ": ");
    13. // store in temp file
    14. out.write(newLine);
    15. out.newLine();
    16. }
    17. else {
    18. // line is okay, store in temp file
    19. out.write(line);
    20. out.newLine();
    21. }
    22. }
    23. // Close all
    24. reader.close();
    25. out.flush();
    26. out.close();
    27. // Delete old players.color and rename temp file
    28. colors.delete();
    29. tempFile.renameTo(colorsFile);
    30. } catch (Exception e) {
    31. log.warning("ColorMe failed to update the colors! Report this please!");
    32. }
    33. }
    34.  
     
  4. Glad it worked :)
     
Thread Status:
Not open for further replies.

Share This Page