Solved FileNotFoundException (access denied)

Discussion in 'Plugin Development' started by CaptainBern, Apr 26, 2013.

Thread Status:
Not open for further replies.
  1. Hi, for the update of my plugin BackPacks++ I made an update tool because the save system has been changed, now what I bascially need to do is creating a folder in a directory, then copy all the files from the dir to the folder. But I'm getting a "FileNotFoundException/access denied" error and I know you will say the file doesn't excist but yes it does, the file is in use, yes but I have no idea on how I can do it using another way, and using a big api like the Apache FileUtils for this seems a bit.."useless?". If you know how to fix this or another way of doing this then please help me. The stacktrace: http://pastie.org/7725208 and my code:

    Code:Java
    1.  
    2. package me.captainbern.backpackupdate;
    3.  
    4. import java.io.File;
    5. import java.io.FileInputStream;
    6. import java.io.FileOutputStream;
    7. import java.io.InputStream;
    8. import java.io.OutputStream;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Update extends JavaPlugin{
    14.  
    15. public void onDisable(){
    16.  
    17. }
    18.  
    19. public void onEnable(){
    20. getLogger().info("Enabled");
    21. File file = new File(Update.class.getProtectionDomain().getCodeSource().getLocation().getPath().replaceAll("%20", " "));
    22. File dir = new File(file.getParentFile().getPath() + File.separator + "BackPacks", "BackPacks");
    23. try{
    24. for (final File player : dir.listFiles()) {
    25. if (player.isDirectory()) {
    26. File backpack = new File(player, File.separator + "BackPack" + File.separator);
    27. getLogger().info("Found a player folder: " + player.getName());
    28. backpack.mkdirs();
    29. getLogger().info("Made new directory...");
    30. getYamls(player, backpack);
    31. }
    32. }
    33. }catch(Exception e){
    34. Bukkit.getLogger().warning("Failed!!");
    35. e.printStackTrace();
    36. }
    37. }
    38.  
    39. private void getYamls(File player, File backpack) {
    40. try{
    41. for(final File file : player.listFiles()){
    42. if(file.isFile()){
    43. if(file.getName().endsWith(".yml")){
    44. getLogger().info("Trying to copy ymls");
    45. copy(file, backpack);
    46. }
    47. }
    48. }
    49. }catch(Exception e){
    50. e.printStackTrace();
    51. }
    52. }
    53.  
    54. public void copy(File sourceLocation , File targetLocation){
    55. try {
    56. InputStream in = new FileInputStream(targetLocation);
    57. OutputStream out = new FileOutputStream(targetLocation);
    58. byte[] buf = new byte[1024];
    59. int len;
    60. while((len=in.read(buf))>0){
    61. out.write(buf,0,len);
    62. }
    63. out.close();
    64. in.close();
    65. sourceLocation.delete();
    66. } catch (Exception e) {
    67. getLogger().info("Failed to copy the Yamls!");
    68. e.printStackTrace();
    69. }
    70. }
    71. }
    72.  
     
  2. Offline

    dark navi

    1) No shame in using Apache FileUtils. They have tons of functions that would be a HUGE pain in the ass to re-create, such as copyDirectory.

    2) If the file is in use, you'd have to release/close it some how before modifying it.
     
    CaptainBern likes this.
  3. The reason I don't want to use fileutils is not because it would be a shame but because in my opnion using an api as big as that just for a plugin of 50 lines.. And I will try the closing..
     
    dark navi likes this.
  4. Offline

    Sagacious_Zed Bukkit Docs

    Bukkit is built with apache commons Lang and Google guava. You can use those without requiring extra libraries.
     
    CaptainBern likes this.
  5. Oh god I'm feeling soo stupid now..I found out why it didn't worked. I tried to copy from a file to a file without there was a file to copy to..I know I said I'm sure it's there but..Thanks for your help!
     
Thread Status:
Not open for further replies.

Share This Page