Not creating file/folder

Discussion in 'Plugin Development' started by JeroenV, Dec 4, 2012.

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

    JeroenV

    Hey, for some reason this code doesn't want to create the folder nor the actual file while I've used stuff like this before and it all worked out fine.

    Here's my code snippet:
    Code:
       
    File Townfolder = new File(getDataFolder(),"TownData");
         
    @SuppressWarnings("unchecked")
    public void OnEnable(){   
       
        YamlConfiguration DefaultConfig = YamlConfiguration.loadConfiguration(new File(Townfolder,"default.yml"));
        Items.add(5);
        Items.add(6);
        Drops.add(5);
        Drops.add(6);
        DefaultConfig.set("Items",Items);
        DefaultConfig.set("Drops",Drops);
        try {
            DefaultConfig.save(new File(Townfolder,"default.yml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
     
  2. Offline

    fireblast709

    afaik for custom folders, you need to call .mkdir()
     
  3. Offline

    adam753

    Creating a new File object just creates a reference to a file with that name in that area, regardless of whether it exists or not. If you then try to use a File object which refers to a file which doesn't exist, you'll get errors.
    You can use Townfolder.exists() to check whether the file exists at that location, and then it's safe to refer to it without getting errors. In the event that it doesn't exist, you can create the file using Townfolder.create().

    Also, I believe you can mark directories in the path string as such: "Townfolder/default.yml". Not sure if that's what you're trying to do here?
     
  4. Offline

    JeroenV

    Ok but even when I manually create the Townfolder it won't create the default.yml.
     
  5. Offline

    adam753

    Code:
    File f = new File(Townfolder, "default.yml");
    f.createNewFile();
    
     
Thread Status:
Not open for further replies.

Share This Page