Solved Put file contents into arraylist?

Discussion in 'Plugin Development' started by PolarCraft, May 11, 2014.

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

    PolarCraft

    Okay so i am taking contents of an arraylist and saving them into a file called "players.txt". Now I am doing this on the disabling of my plugin. Now what i am trying to do is read the files contents and add them to an arraylist on the enabling of the plugin. But that is what i am stuck on.

    Code:
    Code:java
    1. package net.jc.minecraft;
    2.  
    3. import java.io.File;
    4. import java.io.FileInputStream;
    5. import java.io.FileNotFoundException;
    6. import java.io.FileWriter;
    7. import java.io.IOException;
    8. import java.io.ObjectInputStream;
    9. import java.io.PrintWriter;
    10. import java.io.StreamCorruptedException;
    11. import java.util.ArrayList;
    12. import java.util.UUID;
    13.  
    14. import org.bukkit.Bukkit;
    15. import org.bukkit.ChatColor;
    16. import org.bukkit.command.Command;
    17. import org.bukkit.command.CommandSender;
    18. import org.bukkit.entity.Player;
    19. import org.bukkit.event.EventHandler;
    20. import org.bukkit.event.Listener;
    21. import org.bukkit.event.player.PlayerJoinEvent;
    22. import org.bukkit.event.player.PlayerQuitEvent;
    23. import org.bukkit.plugin.java.JavaPlugin;
    24.  
    25.  
    26. public class Main extends JavaPlugin implements Listener {
    27.  
    28. ArrayList<UUID> joinlist = new ArrayList<UUID>();
    29. public void logToFile(ArrayList<UUID> joinlist2)
    30. {
    31. try
    32. {
    33. File dataFolder = getDataFolder();
    34. if(!dataFolder.exists())
    35. {
    36. dataFolder.mkdir();
    37. }
    38. File saveTo = new File(getDataFolder(), "players.txt");
    39. if (!saveTo.exists())
    40. {
    41. saveTo.createNewFile();
    42. }
    43. FileWriter fw = new FileWriter(saveTo, true);
    44. PrintWriter pw = new PrintWriter(fw);
    45. pw.println(joinlist2);
    46. pw.flush();
    47. pw.close();
    48. } catch (IOException e)
    49. {
    50. e.printStackTrace();
    51. }
    52. }
    53. public void onEnable() {
    54. getServer().getPluginManager().registerEvents(this, this);
    55. getCommand("cc").setExecutor(this);
    56. getConfig().options().copyDefaults(true);
    57. saveDefaultConfig();
    58. }
    59.  
    60. @EventHandler
    61. public void joinEvent(PlayerJoinEvent e) {
    62. UUID p = e.getPlayer().getUniqueId();
    63. Player p2 = e.getPlayer();
    64. e.setJoinMessage("");
    65. if(joinlist.contains(p)) {
    66. String JoinEvent1 = ChatColor.translateAlternateColorCodes('&', (String)Main.this.getConfig().getString("old-join-message").replaceAll("%PLAYER%", p2.getName().toString()));
    67. this.getConfig().contains("{PLAYER}");
    68. Bukkit.broadcastMessage(JoinEvent1);
    69. } else {
    70. String JoinEvent2 = ChatColor.translateAlternateColorCodes('&', (String)Main.this.getConfig().getString("new-join-message").replaceAll("%PLAYER%", p2.getName().toString()));
    71. Bukkit.broadcastMessage(JoinEvent2);
    72. joinlist.add(p);
    73. }
    74. }
    75.  
    76. @EventHandler
    77. public void leaveEvent(PlayerQuitEvent e) {
    78. Player p = e.getPlayer();
    79. e.setQuitMessage("");
    80. String LeaveEvent = ChatColor.translateAlternateColorCodes('&', (String)Main.this.getConfig().getString("leave-message").replaceAll("%PLAYER%", p.getName().toString()));
    81. Bukkit.broadcastMessage(LeaveEvent);
    82. }
    83.  
    84. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    85. {
    86. if(cmd.getName().equalsIgnoreCase("cc")) {
    87. joinlist.clear();
    88. }
    89. return false;
    90. }
    91.  
    92. public void onDisable() {
    93. logToFile(joinlist);
    94. }
    95. }
    96.  
     
  2. Offline

    mazentheamazin

    PolarCraft
    You'd use FileReader, and BufferedReader..
    Code:java
    1. public List<String> getContentsFromFile(File f) throws IOException{
    2. //Create a new List to write the contents in
    3. List<String> ls = new ArrayList<String>();
    4. //Create a string that we will add to the list later on
    5. String line;
    6.  
    7. //Create a new BufferedReader and FileReader to read the contents
    8. FileReader fr = new FileReader(f);
    9. BufferedReader reader = new BufferedReader(fr);
    10.  
    11. //Loop through the contents of the file
    12. while((line = reader.readLine()) != null) {
    13. ls.add(line);
    14. }
    15.  
    16. //Close the readers
    17. fr.close();
    18. reader.close();
    19.  
    20. return ls;
    21. }

    Theres another way to do it in Java 7, but some reported of it being buggy.
     
  3. Offline

    PolarCraft

    mazentheamazin How could i put them back into the arraylist joinlist?
     
  4. Offline

    mazentheamazin

    PolarCraft
    Call the method on startup, then loop through the contents of the List<String> and add them to the join list. Keep in mind, if you're using UUIDs, you should be calling UUID.fromString()
     
Thread Status:
Not open for further replies.

Share This Page