Solved Yml file encoding problem ... again ...

Discussion in 'Plugin Development' started by InflamedSebi, Jun 9, 2013.

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

    InflamedSebi

    Hay i use the following method to save my yml configurations to a file:

    Code:java
    1. public boolean saveConfiguration(FileConfiguration fileConfiguration, String fileName) {
    2. File file = new File(p.getDataFolder(), fileName);
    3. if (fileConfiguration == null || file == null)
    4. return false;
    5. try {
    6. fileConfiguration.save(file);
    7. return true;
    8. } catch (IOException ex) {
    9. return false;
    10. }
    11.  
    12. }


    but bukkit converts the output file to an ANSI encoded file ... any ideas how to fix this?
    i want my files to stay UTF-8 ...

    Ok seems like i fixed it:

    Code:java
    1.  
    2. /**
    3.   * Returns if the values from a yml cofiguration were saved to the file inside the plugin data folder. If the file does not exist, or could not be saved it will return false.
    4.   *
    5.   * @param configFile
    6.   * @param fileConfiguration
    7.   * @return Boolean success
    8.   */
    9. public boolean saveConfiguration(FileConfiguration fileConfiguration, String fileName) {
    10. File file = new File(p.getDataFolder(), fileName);
    11. if (fileConfiguration == null || file == null)
    12. return false;
    13. String fileconfigString = fileConfiguration.saveToString();
    14. try {
    15. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF-8")));
    16. char[] buffer = new char[1024];
    17. try {
    18. for (int buf = 0; buf < fileconfigString.length();) {
    19. int endIndex = (buf + buffer.length > fileconfigString.length()) ? fileconfigString.length() : buf + buffer.length;
    20. buffer = fileconfigString.substring(buf, endIndex).toCharArray();
    21. writer.write(buffer, 0, buffer.length);
    22. buf += buffer.length;
    23. }
    24. } finally {
    25. writer.close();
    26. }
    27. } catch (Exception e) {
    28. e.printStackTrace();
    29. return false;
    30. }
    31. return true;
    32.  
    33. }
    34.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page