How to create a new file then store stuff in it

Discussion in 'Plugin Development' started by bowlerguy66, Aug 27, 2015.

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

    bowlerguy66

    I am trying to make a separate file than the default config. I have all of this but when I try to use it it doesn't return anything.

    Code:
        public File getBlocks() {
            File blocks = new File("blocks.yml");
            return blocks;
        }
    
     
  2. Offline

    SkyleTyler1337

    @bowlerguy66
    1. check if the data folder exist's.
    2. if it doesn't make the directory.
    3. create the blocks.yml file
    Code:
    public void loadBlocks(){
       File dataFolder = this.getDataFolder();
    
     /** make the data folder if it doesn't exist already*/
       if(!dataFolder.exists()){
            dataFolder.mkdir();
    }
    
        File blocks = new File(dataFolder, "blocks.yml");
      if(!blocks.exists()){
           try{
            blocks.createNewFile();
             }catch(IOException e){
               e.printStackTrace();
        }
      }
    }
     
  3. Offline

    teej107

  4. Offline

    au2001

    @SkyleTyler1337 File#isDirectory() and File#isFile() would be more appropriate I think.


    @teej107 Explain? If you create a new file each time the server starts without checking if it already exists, it will probably throw an error in the console, which nobody likes.
    The check before mkdirs is probably useless though.
     
  5. Offline

    Xerox262

    The method File#createNewFile() returns false if the file already exists. It only throws an IOException if the path specified is invalid
     
  6. Offline

    au2001

    @Xerox262 Oh okay, didn't know that (and didn't really have time to go read the documentation).
     
Thread Status:
Not open for further replies.

Share This Page