Thread: File writing and reading for ArrayLists

Results 1 to 9 of 9
  1. #1 File writing and reading for ArrayLists 
    Member

    Join Date
    Sep 2007
    Posts
    614
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Well, I saw a thing about "codes" stored in a text file, where users are given codes and then they can use them to redeem things. So, I wrote some stuff up using a text file and an array list.
    I give you the basic methods for this.

    Code:
        public static boolean useCode(String toRemove) {
            if (Server.codes.contains(toRemove)) {
                if (Server.codes.remove(toRemove)) {
                    arrayToFile(Server.codeLocation, Server.codes);
                    return true;
                }
            }
            return false;
        }
        public static void fileToArray(String file, ArrayList array, boolean debug) {
            try {
                File theFile = new File(file);
                if (file == null) {
                    if (debug) {
                        System.out.println("[fileToArray] File is null");
                    }
                    return;
                }
                array.clear();
                BufferedReader br = new BufferedReader(new FileReader(file));
                String line;
                while ((line = br.readLine()) != null) {
                    array.add(line);
                    if (debug) {
                        System.out.println("Added : " + line);
                    }
                }
                if (debug) {
                    System.out.println("List contains " + array.size() + " entries");
                }
                br.close();
                if (debug) {
                    System.out.println("[fileToArray] Loaded " + array.size() + " entries");
                }
            } catch (IOException ex) {
                //Logger.getLogger(manager.class.getName()).log(Level.SEVERE, null, ex);
                if (debug) {
                    System.out.println("[fileToArray] Error loading file : " + file);
                    ex.printStackTrace();
                }
            }
        }
    
        public static void arrayToFile(String file, ArrayList array) {
            try {
                FileWriter outFile = outFile = new FileWriter(file);
                PrintWriter out = out = new PrintWriter(outFile);
                for (int i = 0; i < array.size(); i++) {
                    out.println(array.get(i));
                }
                out.close();
                outFile.close();
            } catch (IOException ex) {
                //Logger.getLogger(manager.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("[arrayToFile] Error writing to file : " + file);
                ex.printStackTrace();
            }
        }
    Basically you setup your file location and array list, when your server starts, load your array list from a file using the methods, then in commands or whatever, use useCode as a boolean, so if the boolean is true, you give the player a reward, if its false, the code does not exist. If it is true, the item is removed from the array and the array is saved.

    An example command for this is
    Code:
    if (cmd.startsWith("code")) {
                    String entry = cmd.substring(5);
                    if (Misc.useCode(entry)) {
                        c.Send("The code you redeemed is correct!");
                        c.addItem(995, 100000);
                    } else {
                        c.Send("Sorry, the code you have entered is invalid");
                    }
                }
    So if the array (loaded from the text file) contains the code, the user is given the whatever, and the item is removed from the array, and then it is re-saved to the file.


    Expanded uses :
    Have it read from a web site, and on the website, use a donation system? or perhaps some type of forum integration?
    Comments? Criticism?
    Reply With Quote  
     

  2. #2  
    Expert Programmer


    Join Date
    Dec 2007
    Posts
    2,018
    Thanks given
    52
    Thanks received
    84
    Rep Power
    986
    Uhh, Whats the point of using anything to store data if it is constantly being loaded? An ArrayList would be ideal if it was loaded once at startup or something.
    Reply With Quote  
     

  3. #3  
    Member

    Join Date
    Sep 2007
    Posts
    614
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Quote Originally Posted by Ultimate View Post
    Uhh, Whats the point of using anything to store data if it is constantly being loaded? An ArrayList would be ideal if it was loaded once at startup or something.
    Basically you setup your file location and array list, when your server starts, load your array list from a file
    Thats exactly what I said...It loads ONCE at the begging of the server, because any changes will affect the servers array list and only write to it if a change is made, so the file is updated with the server array list status, to ensure you dont have any leaks.
    Reply With Quote  
     

  4. #4  
    Expert Programmer


    Join Date
    Dec 2007
    Posts
    2,018
    Thanks given
    52
    Thanks received
    84
    Rep Power
    986
    Oh lol my bad, all that text and code wasn't easy on the eyes :/
    Reply With Quote  
     

  5. #5  
    Member

    Join Date
    Sep 2007
    Posts
    614
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Quote Originally Posted by Ultimate View Post
    Oh lol my bad, all that text and code wasn't easy on the eyes :/
    Ahh, its all good.
    But aside from that, is it good?
    I know there are efficiency nuts around here, and, I was just experimenting and I made this. Im probably going to use it for my server...somehow.

    Maybe something that picks out a random key as a prize.
    Reply With Quote  
     

  6. #6  
    Expert Programmer


    Join Date
    Dec 2007
    Posts
    2,018
    Thanks given
    52
    Thanks received
    84
    Rep Power
    986
    You've done a good job If I were to do anything differnt, I'd use XML I XML
    Reply With Quote  
     

  7. #7  
    Member

    Join Date
    Sep 2007
    Posts
    614
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Quote Originally Posted by Ultimate View Post
    You've done a good job If I were to do anything differnt, I'd use XML I XML
    Im honestly not sure how to use it =p but I'll probably make that my next experiment.
    Reply With Quote  
     

  8. #8  
    Member Market Banned Market Banned

    Zee Best's Avatar
    Join Date
    Feb 2007
    Age
    29
    Posts
    3,036
    Thanks given
    24
    Thanks received
    210
    Rep Power
    1171
    Why do you declare a new instance of a class as null then create the instance, why not just initiate it straight away.

    Also, you need to clean up your code.


    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  9. #9  
    Member

    Join Date
    Sep 2007
    Posts
    614
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Quote Originally Posted by Zee best View Post
    Why do you declare a new instance of a class as null then create the instance, why not just initiate it straight away.

    Also, you need to clean up your code.
    Its the way my IDE did it. It said it needed to be surrounded with Try-catch, so I hit surround block, and it just did it this way. When Im experimenting its not always about the neatness, but, I agree, it does need to be cleaned up, so, Ill do that for my version, as for the rest of you, this is a snippet =p your going to modify it anyway so clean it up yourselves.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •