Write/Read ArrayList

Discussion in 'Plugin Development' started by edocsyl, Jun 18, 2012.

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

    edocsyl

    Hi

    First: I wanna store each player who join in a array list and save them in a file as object like so:

    PHP:
        private void saveList(){
            
    File joinListFile = new File(getDataFolder().getPath(), "joinList.yml");
            try {
                
    FileOutputStream fops = new FileOutputStream(joinListFile);
                
    ObjectOutputStream oos = new ObjectOutputStream(fops);
                
    oos.writeObject(joinList);
            } catch (
    FileNotFoundException e) {
                
    log.info("Cant save joinList to \"joinList.yml\" file!");
            } catch (
    IOException e) {
                
    log.info("Cant save joinList to \"joinList.yml\" file!");
            } 
        }
    This works without a problem. Now i wanna read them and create a arraylist object. And this doesn't work.

    PHP:
        public void readList(){
            
    File joinListFile = new File(getDataFolder().getPath(), "joinList.txt");
         
            if(
    joinListFile.exists()){
                try {
                    
    FileInputStream fips = new FileInputStream(joinListFile);
                    
    ObjectInputStream oips = new ObjectInputStream(fips);
                    try {
                        
    joinList =  (ArrayList<String>) oips.readObject();
                    } catch (
    ClassNotFoundException e) {
                        
    log.info("Cant read joinList from \"joinList.yml\" file!" e);
                    }
                } catch (
    FileNotFoundException e) {
                    
    log.info("Cant read joinList from \"joinList.yml\" file!" e);
                } catch (
    IOException e) {
                    
    log.info("Cant read joinList from \"joinList.yml\" file!" e);
                }
            }
         
        }

    What did i wrong, or is there a easyer method to save players ?
     
  2. Offline

    ItsHarry

    "This doesn't work".
    Yeah, that really helps...

    Show us some errors, or atleast explain why it's not working..
     
  3. Offline

    edocsyl

    Sry, there are no errors, maybe made a mistake in the player listener.

    PHP:
    if(!plugin.joinList.contains(p.getName())){
                        
    //DO Stuff cuz 1st join!
                        
    plugin.joinList.add(p.getName());
                    }
    EDIT:
    I think in the readList() function is the problem
     
  4. Offline

    Njol

    Just compare these two lines from the codes you posted:
    BTW: you should not save an object as a .yml as .yml is used for YAMl files already, but use e.g. '.obj' or something similar.
     
  5. Offline

    edocsyl

    Yeah. Thx Njol, I don't see anything if i watch since 8 hours in the screen -.-
     
Thread Status:
Not open for further replies.

Share This Page