1.15.2 Deleting a YML file in code

Discussion in 'Spigot Plugin Development' started by WhispTheFox, Mar 23, 2020.

  1. Hiya,

    I'm trying to delete yml files after a command is executed. I currently have these files being generated through an abstract file class of my own design. I have a method that should delete the file, but it doesn't. The reason why is because the file is still held in memory. Is there any way to make sure the file is unloaded so it can be removed? Here is my abstract file class:

    Code (Java):
    abstract class AbstractFile {

        private AndromedaShops plugin;
        private File file;
        protected FileConfiguration config;
        private File dir;
        private String filename;

        public AbstractFile(AndromedaShops pl, String filename, String d){
            plugin = pl;
            this.filename = filename;
            File dir = new File(pl.getDataFolder() + d);
            if (!dir.exists()){
                dir.mkdirs();
            }
            this.dir = dir;
            file = new File(dir, filename);
            if (!file.exists()){
                try{
                    file.createNewFile();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
            config = YamlConfiguration.loadConfiguration(file);
        }
        public void save(){
            try{
                config.save(file);
            }catch(IOException e){
                System.out.println("Could not save file");
            }
        }
        public FileConfiguration get(){
            return config;
        }

        public void reload(){
            config = YamlConfiguration.loadConfiguration(file);
        }

        public void delete(){
            config = null;
            file.delete();
        }
    }
     
  2. You mean delete from the jar or from the folder?
     
  3. From the folder. None of my config yml files are stored in the jar.
     
  4. Once a instance of a file is created you can freely delete it by simply using
    Code (Text):
    file.delete();
     
  5. If you looked at the code at all, you would see that there is a call to File#delete. If you actually read the question, I'm looking for a way to make sure that the file is unloaded from memory so that calling File#delete doesn't return false.
     
  6. It's pretty obvious isn't it? Ur already loading it into memory once you construct the class
    Code (Text):
    File dir = new File(pl.getDataFolder() + d);
     
  7. The File class is just an abstract representation of a file that might or might not be on the file system and is not holding any resources / streams open to it. Therefore just holding onto a File object itself will not cause the whole actual file or references to it to be kept in memory.

    This probably is a case of the file being read into memory through the use of FileConfiguration (which obviously loads the file through one or another resource / stream to the actual file) and even if it has closed its resources / streams to the file already, you might just run into the case of them still being present in memory, even tho FileConfiguration has already "closed" them properly and you having "deleted" the reference to the configuration object by setting it to null.

    If this is the case than the garbage collector might has just not taken care of the left-overs yet and you have to either "wait" until this happens automatically or force a garbage collection and therefore the dumping of possible memory references to the file, by calling:
    Code (Java):
    System.gc();
    If you got "rid" of all references to the file (except for the File objects, these are okay to keep) and you can still not delete the file, even after forcing garbage collection. Something is really wrong.
     
  8. System.gc() does not force garbage collection, it hints the JVM to perform garbage collection and returns from the method after the best effort to collect the garbage has been made. There is no guarantee that it will actually collect the garbage.
     
  9. Forcing might have been the wrong word. But if it's really a matter with garbage collection and the file still being stuck in memory, it's your best shot anyways I guess.
    At least if I'm not missing something.