Copying, saving config

Discussion in 'Plugin Development' started by Dreeass, Jun 30, 2012.

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

    Dreeass

    In my plugin I'm copying a world from a folder to another folder in the main folder of the server (where the craftbukkit jar is located). And then copy the config from within that folder to the data folder of the plugin, he copies the world correctly but copying the config doesn't go as planned, he needs to get data from the config.yml from the data folder but it's not doing it correctly.

    Here's my code:
    Code:java
    1. public void copyConfig() throws IOException {
    2.  
    3. File srcFile = new File(plugin.getConfig().getString("CopyToWorld") + "/" + "config.yml");
    4. File destFile = new File(plugin.getDataFolder() + "/config.yml");
    5.  
    6. InputStream in = new FileInputStream(srcFile);
    7. OutputStream out = new FileOutputStream(destFile);
    8.  
    9. byte[] buffer = new byte[1024];
    10.  
    11. int length;
    12.  
    13. while((length = in.read(buffer)) > 0) {
    14. out.write(buffer, 0, length);
    15. }
    16.  
    17. in.close();
    18. out.close();
    19. plugin.saveConfig();
    20. plugin.reloadConfig();
    21. plugin.log.info("The config has been successfully reloaded!");
    22.  
    23. }
     
  2. 1. I think you mispplaced the destination and the sourche file, you copying it from the new world to the plugin
    2. you not closing the 'in' if an error ocur = file resource leak
    3. you not closing the 'out' if an error ocur
    4. its better to wrap native stream into buffered streams to prevent the underlying system cals
     
  3. Offline

    Dreeass

    How would I check if an error occurs and close it?
     
  4. use the try and finaly blocks
    Code:java
    1.  
    2. InputStream in = null;
    3. OutputStream out = null;try{
    4. in = new BufferedInputFile(new FileInputStream(srcFile));
    5. out= new BufferedOutputStream(new FileOutputStream(destFile));
    6. byte[] buffer = new byte[1024];
    7.  
    8. int length;
    9.  
    10. while((length = in.read(buffer)) > 0) {
    11. out.write(buffer, 0, length);
    12. }
    13. }finally{
    14. if(in!=null)in.close();
    15. if(out != null) out.close();
    16. }
     
Thread Status:
Not open for further replies.

Share This Page