Solved Check for a file

Discussion in 'Plugin Development' started by ulsa, Jun 24, 2014.

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


    Well guys while messing with saving stuff i failed to use java i/o on files

    added this so it will check for the following path


    and write true or false on start

    but even if i have it deleted or not deleted on load it allways gives me false.. why
    and for the ones asking for entire code .. its soo messy and buggy but just doing it for test :
    *done uploading*
  2. Offline


    You shouldn't ask the operating system for "user.dir," instead ask Bukkit for JavaPlugin#getDataFolder() to get a directory to save in.
  3. Offline


    Use Bukkit's built-in method: .getDataFolder()
  4. Offline


    well its the same directory "also for the ones asking why do you use / .. java autocorrects it to \"

    also chat output at start gives correct path but false

    and also how can i use javaplugin.getdatafolder();
    i looked at the page and tried "plugin.getDataFolder();" but didnt worked
  5. Offline


    You can access the method from your main class, the one that extends JavaPlugin.
  6. Offline


    Thanks for the replies and the great help
    i found both ways and how to do it in following
    and hopefully this will help to the people like me who want to start from 0

    1st way ; by getting servers location from windows' own dir "not by bukkit"

    1. File file = new File(System.getProperty("user.dir") +"/plugins/Hometest/homes.yml");
    2. boolean exists = file.exists();
    3. Bukkit.broadcastMessage("File " + file.getPath() + " exists: " + exists);

    note that System.getProperty("user.dir") gives you the servers folder not the plugins folder so you need to add /plugins again...

    also hometest was my folder and homes.yml was my file

    and 2nd way ; With bukkits own method "using bukkit"

    1. File file2 = new File(getDataFolder() +"/homes.yml");
    2. boolean exists2 = file2.exists();
    3. Bukkit.broadcastMessage("File " + file2.getPath() + " exists: " + exists2);

    getdatafolder will give you the direct folder of your plugin under /plugins/ so you can add your yml to check :)
  7. Offline


    You can try a test statement:

    1. if (file.exists()) {
    2. Bukkit.broadcastMessage("File " + file.getPath() + " exists: true");
    3. } else {
    4. Bukkit.broadcastMessage("File " + file.getPath() + " exists: false");
    5. }
Thread Status:
Not open for further replies.

Share This Page