Solved Unable to delete file

Discussion in 'Plugin Development' started by wesley27, Jun 26, 2015.

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

    wesley27

    Hey everyone, just having a small issue with deleting files. We just redid an old inventory system, and are now using new filetypes. Everything in converting the files is working, except deleting the old ones. I'm really not sure why, any help is appreciated, thanks! (This is an excerpt from the code)
    Code:
    public void run() {
        File file = new File("plugins/ou/inv/"+player.getName()+".inv");
        if (file.exists()){
    
            //Clear inv to be safe
            player.getInventory().clear();               
            String output;
            try {
                Scanner scanner = new Scanner(file);
                output = scanner.next();
                final Inventory inv = InventoryStringDeSerializer.StringToInventory(output);
                scanner.close();
                file.delete();                                             
                //Send result to game
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    public void run() {
                        player.getInventory().setContents(inv.getContents());
                    }
                });
            } catch (FileNotFoundException e) {e.printStackTrace();}
        } else {
            player.sendMessage(ChatColor.DARK_RED + "You do not have a Survival inventory to recover");
        }
    }
     
  2. Offline

    Funergy

    @wesley27
    You cannot delete a file thats opened or if the folder where it is in is opened.
     
  3. Offline

    wesley27

    @Funergy Thats what I thought, which is why I tried to close the scanner as well. However, I couldn't figure out a way to close the file, since there is no file.close() that you're able to use there. How would I go about closing it?
     
  4. Offline

    Funergy

    @wesley27 Do you have the folder opened with all these files when they are getting deleted?
    Maybe thats the problem.
    I've had this too on my Mac.
    Whenever I tried to delete something whilst I have the folder opened where it is in I got a Java error.
    You are probably not even seeing the error because you only catch the file not found exception.

    And try to set the delete line with a little delay.
     
  5. Offline

    wesley27

    @Funergy Well, it runs that code when you execute a command. It's basically transferring old inventory files to new ones, and then its supposed to delete the old inventory file so you can't duplicate the inventory. They don't delete whether the folder is closed or open, I just tried to be sure. And you're correct, there are no errors at all in the console which is why I am not sure what to do :/

    I tried adding a delay, it still didn't delete.

    @Funergy Actually, I got it working with a delay! I think it just needed more of a delay than I was giving it, and I redid a bit of the code to close the scanner and all. Thanks for the help :)

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

Share This Page