folder doesnt create on first run.

Discussion in 'Plugin Development' started by TopGear93, Jun 14, 2012.

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

    TopGear93

    Why does this code make a directory on the second run??

    Code:
        public void create(){
            File dir = new File(plugin.getDataFolder() + "\\database");
            boolean success = dir.mkdir();
            if(success){
                System.out.print("[TrialPlay]-Folder created");
            }else {
                System.out.println("[TrialPlay]-Folder failed like usual!");
            }
        }
    
     
  2. Offline

    r0306

    TopGear93
    mkdir just creates the directory that was defined. You have to use the createNewFile() method.

    Code:
    public void create(){
            File dir = new File(plugin.getDataFolder() + "\\database");
            boolean success = dir.mkdir();
            dir.createNewFile();
            if(success){
                System.out.print("[TrialPlay]-Folder created");
            }else {
                System.out.println("[TrialPlay]-Folder failed like usual!");
            }
        }
     
    TopGear93 likes this.
  3. Offline

    TopGear93

    thats what ive been trying too. It only errors out saying the directory cant be found.
     
  4. Offline

    r0306

    TopGear93
    Use this:
    Code:
    File dir = new File(plugin.getDataFolder().getName() + "\database");
     
    TopGear93 likes this.
  5. Offline

    imjake9

    That's Windows-specific, and it's still not the best practice. This code will take any file and ensure its creation:
    Code:java
    1. File f = new File(plugin.getDataFolder(), "filename.txt");
    2. if (!f.exists()) {
    3. f.getParentFile().mkdirs();
    4. try {
    5. f.createNewFile();
    6. } catch (IOException ex) {
    7. // Handle error
    8. }
    9. }
     
    TopGear93 likes this.
  6. Offline

    TopGear93

    This works perfectly. I also figured out how to make a directory within TrialPlay's folder with this. r0306 & imjake9 Thank you for the help.
     
Thread Status:
Not open for further replies.

Share This Page