Solved Cannot create a file

Discussion in 'Spigot Plugin Development' started by UnspecifiedUser, Jun 30, 2020.

  1. Hello,

    I tried in a test plugin to create a json file on startup containing 3 items. I am not sure this is the best way to store items, and I have no idea if ItemStack would be well serialized. But the problem is that I cannot create the file. I've already created files, so I don't really know what I messed up.
    And I if manually create the file, it is deleted after startup.

    Code (Text):

    public void onEnable()
       {
           try
           {
               File file = new File(getDataFolder() + File.separator + "items.json");
           
               if (!file.exists())
                   file.createNewFile();
           
               ItemStack item1 = new ItemStack(Material.ANVIL);
               ItemMeta im1 = item1.getItemMeta();
               im1.setDisplayName("renamed anvil");
               item1.setItemMeta(im1);
           
               ItemStack item2 = new ItemStack(Material.APPLE);
           
               ItemStack item3 = new ItemStack(Material.POTION);
               PotionMeta pm = (PotionMeta) item3.getItemMeta();
               pm.addCustomEffect(new PotionEffect(PotionEffectType.ABSORPTION, 200, 6), true);
               item3.setItemMeta(pm);
           
               ArrayList<ItemStack> list = new ArrayList<>(3);
               list.add(item1);
               list.add(item2);
               list.add(item3);
           
               File temp = File.createTempFile(file.getName(), null);
               FileWriter writer = new FileWriter(temp);
           
               Gson gsonE = new GsonBuilder().serializeNulls().create();
               gsonE.toJson(list, writer);
               writer.flush();
               writer.close();
           
               file.delete();
               temp.renameTo(file);
           
           
           } catch (IOException e)
           {
               e.printStackTrace();
           }
       }
     
     
    #1 UnspecifiedUser, Jun 30, 2020
    Last edited: Jun 30, 2020
  2. TheViperShow

    TheViperShow Previously FendiTony777

  3. It's been a while, but I'm pretty sure I had to delete the original file first, and then rename the temp file with the original file name
    I tried to remove this line (the delete()), but all I have is a blank file, even if I replace the ArrayList of ItemStack by an ArrayList of strings
     
  4. there is some weirdness here I think
    • You are creating a file at the beginning
    • You create another file (no need for createtempfile here, you could just use the createnewfile method)
    • Then you rename the temp file to the name of the first file
    • Then you delete the file with that name

    Maybe delete the file first and then rename the other file

    also, I would suggest using the version of the toJson method which returns the json string and then write it to the file instead of passing the writer to gson


    Edit: if you just want to edit the contents of the original file you can simply overwrite it, no need to create temp files and bother with renaming and deleting
     
    • Like Like x 1
  5. That was a messy code, thanks for your help
     
    #5 UnspecifiedUser, Jun 30, 2020
    Last edited: Jun 30, 2020