Solved Remove a file's content

Discussion in 'Plugin Development' started by Robin Bi, Nov 15, 2014.

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

    Robin Bi

    Hey there!


    So I'm currently working with YAML-Files. In the project i'm coding, i need to rewrite the contents of those files very often because i have to synch them to an ArrayList i have in the code.
    Therefor, i have to remove the file's content. I have googled it, but I haven't found anything but "delete the file and create it again", but then i lose an object i've made before what would end up in a) plenty of NPEs and b) a bunch of senseless RAM-usage.


    Maybe i'm totally wrong and that's the simplest thing on earth, but then please wise me up :)
    Thanks <3


    ~Robin Bi
     
  2. Robin Bi
    According to a 30 second search on google:
    Code:
    PrintWriter pw = new PrintWriter("filepath.txt");
    pw.close();
    By writing nothing to a file, it should (in theory) completely erase the file's content.
     
  3. Offline

    Robin Bi

    DJSkepter First, thank you for your answer.
    I don't really know if i've got you correctly, so i've just made some experimental code:

    Code:java
    1. public class Main extends JavaPlugin {
    2.  
    3. File rawFile;
    4. FileConfiguration file;
    5. String fileName;
    6.  
    7. public void onEnable() {
    8. if (!this.getDataFolder().exists()) this.getDataFolder().mkdir();
    9. }
    10.  
    11. @Override
    12. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    13. if (cmd.getName().equalsIgnoreCase("file")) {
    14. fileName = args[0];
    15.  
    16. rawFile = new File(this.getDataFolder(), fileName + ".yml");
    17.  
    18. if (!rawFile.exists()) {
    19. try {
    20. rawFile.createNewFile();
    21. } catch (Exception e) {
    22. e.printStackTrace();
    23. }
    24. }
    25.  
    26. file = YamlConfiguration.loadConfiguration(rawFile);
    27. }
    28.  
    29. else if (cmd.getName().equalsIgnoreCase("clear")) {
    30. try {
    31. PrintWriter pw = new PrintWriter(fileName + ".yml");
    32. pw.close();
    33. } catch (FileNotFoundException e) {
    34. e.printStackTrace();
    35. }
    36. }
    37.  
    38. return true;
    39. }
    40.  
    41.  
    42. }
    43.  

    (Yes i know i have done no null checks and stuff, but this was just a test.)

    /file works perfectly fine, but i'm about to get to know how to clear files, and /clear unfortunately does nothing. Have i used pw like you wanted me to?



    ~Robin Bi

    DJSkepter So I've just done it like this:
    Code:java
    1. rawFile.delete();
    2.  
    3. try {
    4. rawFile.createNewFile();
    5. } catch (IOException e) {
    6. Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Could not create " + fileName + ".yml!");
    7. }

    Even though I had to restructure a hell of a lot code now, but it works.


    Thank you, DjSkepter. <3

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  4. Offline

    Skionz

    Robin Bi likes this.
Thread Status:
Not open for further replies.

Share This Page