API ArrayList to file thing and back

Discussion in 'Resources' started by Skionz, Nov 10, 2014.

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

    Skionz

    This class saves array lists into files and lets you read them easily.
    Code:java
    1. public class ListFile {
    2. File file;
    3. String path;
    4.  
    5. public ListFile(String path, String extension) {
    6. this.path = path + "." + extension;
    7. this.file = new File(this.path);
    8. try {
    9. this.file.createNewFile();
    10. } catch (Exception e) {
    11. e.printStackTrace();
    12. }
    13. }
    14. public ArrayList<String> read() {
    15. ArrayList<String> list = new ArrayList<String>();
    16. try {
    17.  
    18. String line = null;
    19. while ((line = br.readLine()) != null) {
    20. list.add(line);
    21. }
    22. br.close();
    23. } catch(Exception e) {
    24. e.printStackTrace();
    25. }
    26. return list;
    27. }
    28. public void write(ArrayList<String> list) {
    29. try {
    30. BufferedWriter bw = new BufferedWriter(new FileWriter(path, true));
    31. for(String line : list) {
    32. bw.append(line);
    33. bw.newLine();
    34. }
    35. bw.close();
    36. } catch(Exception e) {
    37. e.printStackTrace();
    38. }
    39. }
    40. }

    Here is some example usage
    Code:java
    1. ListFile file = new ListFile("/path/to/file", "txt");
    2. ArrayList<String> list = new ArrayList<String>();
    3. list.add("Welcome to the server!");
    4. list.add("Use /help for help");
    5. file.write(list);

    This writes the array list to the file. The file "file.txt" would look like this
    Code:
    Welcome to the server!
    Use /help for help
    To get the array list from the file you could do this. The read() method returns an array list that you can iterate through.
    Code:java
    1. ListFile file = new ListFile("/path/to/file", "txt");
    2. for(String line : file.read()) {
    3. System.out.println(line);
    4. }

    The output would be
    Code:
    Welcome to the server!
    Use /help for help
    This could be useful for motds and stuff I guess. I just started using buffered streams so comment if I did it wrong :p
     
    ChipDev likes this.
  2. Offline

    Gamecube762

    um..
    getConfig().set("path.to.list", ArrayList<String>);
    getConfig().getList("path.to.list");
    ?
     
  3. Offline

    xTrollxDudex

    You mean saving only String lists to a file?
     
    Skionz, Gamecube762 and teej107 like this.
  4. Offline

    Skionz

    My method doesn't require the bukkit api so you can use it in other projects as well
     
  5. Offline

    teej107

    You can download YMLs api.
     
    Gamecube762 and Skionz like this.
  6. Offline

    ChipDev

    What if you can't download stuff?
    *You just got owned.
     
  7. Offline

    mrCookieSlime Retired Staff

    And how did you post this then...?
     
  8. Offline

    ChipDev

    I am talking about people who cannot. :p
     
  9. Offline

    Gamecube762

    Then they won't need this as they won't be able to download Java, an IDE, or Minecraft.
     
  10. Offline

    ChipDev

    Lets talk about my friend Ian who's computer crashed after he got all that stuff.
    Now he can't download stuff.
    Rare thing..
    but... Billions of people.
     
  11. Offline

    _Filip

    Gamecube762 lol if they couldn't download things then they couldn't even see this resource.
     
  12. Offline

    Gamecube762

    I've gone through multiple computer crashes and recovered them. If your friend attempts to download stuff with a crashed computer, I think he needs to get his priority straight...

    This conversation is off-topic... I think we need to return to the topic.
     
  13. Offline

    ChipDev

    Yep.
    On-topic by telling him how to Improve the API, because he is being helpful.
     
  14. Offline

    teej107

    If you can't download stuff, then you need to talk the the website owner, your ISP, or your computer admin.

    https://code.google.com/p/snakeyaml/ Here is the YAML api that Bukkit uses (I think, or at least a variation of). The download link for that works fine.
     
Thread Status:
Not open for further replies.

Share This Page