Creating Folders.

Discussion in 'Plugin Development' started by Laxer21117, Apr 15, 2014.

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

    Laxer21117

    Hi. I made an auto broadcast plugin and I want it to generate a folder with files I have made in it but I don't know how. Please help asap.
     
  2. Offline

    coasterman10

    Look into the File class. This works with both folders and files. To create a folder, you would create a File object, and then invoke mkdir() on it. In the context of Bukkit plugins, you probably want to put it inside your plugin's data folder, so this would do the trick:
    Code:java
    1. File folder = new File(getDataFolder(), "folderName");
    2. folder.mkdir();


    If you are not entirely sure that your plugin's data folder exists yet, you can also use mkdirs() which will also create the plugin's data folder if it doesn't already exist.
     
  3. Offline

    Laxer21117

    coasterman10 So when I make this it will generate a folder on installation with files in it? Sorry this is my first plugin that has files. Also how would I put the files in the folder?
     
  4. Offline

    coasterman10

    When the plugin starts up, make the File object, and then use its exists() method to see if it exists. If not, then go ahead and create it. This should actually be done with the plugin's data folder too before anything else. You will also probably need some try-catch statements to catch IOExceptions if the files can't be made. I forget where exactly these should go but your IDE should give you errors and suggested fixes as to where to put the try-catch blocks.

    To get/put files within a folder, you can use File again, except this time you pass your folder into the constructor, followed by the filename:
    Code:java
    1. File folder = new File(getDataFolder(), "folderName");
    2. File file = new File(folder, "fileName");

    Of course you will still have to check if the files exist and call their respective functions to create them. To create a regular file, use createNewFile().
     
  5. Offline

    Laxer21117

    coasterman10 So I would use that code but change "filename" with "config.yml" and I would add the config.yml to the project first?
     
  6. Offline

    coasterman10

    What I provided is an example. The File object is not to be confused with the actual file on the disk. Simply calling the constructor won't actually create the file; you need to run the necessary method to create the file.

    If you just want to use the config.yml within the plugin's root data directory (which has the same name as the plugin and is within the plugins folder), then you should be using getConfig(). Remember how the data hierarchy works. There is the base plugins folder, and then each plugin within can have a folder by its name (which is getDataFolder()). The usual config.yml sits in here. If you want a folder within that folder, then the code I am providing becomes relevant.

    I'm not entirely clear on what you're trying to do with these multiple files. Are you looking to create multiple files or do you just want one config file?
     
  7. Offline

    Laxer21117

    coasterman10 I am looking to make multiple files but I don't know how to make a plugin generate a folder called the plugin name with files that I have created like messages.txt.
     
  8. Offline

    coasterman10

    In that case, you just need to use getDataFolder() and then you can work with that. Check if it exists with exists(), and if not, use mkdir() to create it. Then you can create files within that as I showed you earlier. It would work something like this:
    Code:java
    1. // inside your onEnable()
    2.  
    3. if (!getDataFolder().exists())
    4. getDataFolder().mkdir();
    5.  
    6. File messages = new File(getDataFolder, "messages.txt");
    7. if (!messages.exists())
    8. messages.createNewFile();

    Note that you will probably need a try-catch block somewhere in this code.
     
    Laxer21117 likes this.
Thread Status:
Not open for further replies.

Share This Page