简体   繁体   中英

How to read an ArrayList from a File into another ArrayList?

I trying to read an arraylist from a file into another arraylist but I keep getting errors. The file is called eventos.dat and the arraylist is from the type Evento . I want to create a new ArrayList<Evento> with the objects from the array on the file. Here is the method i'm using:

public class ListaEventos implements Serializable{
    private  ArrayList<Evento> eventos = new ArrayList();

    public String adicionarEvento(Evento novo){
         for (Evento evento : eventos) {
             if(novo.equals(evento)){
                return "Evento já existe";
            }     
        }
        eventos.add(novo);
        return "ADICIONEI";

    }


    public  ArrayList<Evento> getEventos() {
        return eventos;
    }

    public Evento procuraEvento(String tituloEvento){
        for (Evento evento : eventos){
            if(tituloEvento.equals(evento.getTitulo())){
                return evento;
            }
        }
        return null;
    }

    public String editaEvento(Evento antigo, Evento novo){
        for (int i=0;i<eventos.size();i++){
            if(antigo.equals(eventos.get(i))){
                eventos.get(i).setTitulo(novo.getTitulo());
                eventos.get(i).setData(novo.getData());
                eventos.get(i).setDescricao(novo.getDescricao());
                eventos.get(i).setLocal(novo.getLocal());
                eventos.get(i).setPrivado(novo.getPrivado());
                return "Editei evento";
            }
        }
        return "Evento não existe";
    }

    public String removeEvento(String removeTitulo){
        Evento aux= procuraEvento(removeTitulo);

        if(aux != null){
            eventos.remove(aux);
            return "Evento removido!";
        }
        return "Evento não existe";
    }



    public void gravaFicheiro(){
        try{
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Eventos.dat"));
            out.writeObject(eventos);
            out.close();
        }
        catch(IOException ex){
            System.out.println("Não conseguiu gravar");
        }
    }

    public ArrayList<Evento> carregaEventos() throws ClassNotFoundException{

       try{

            ObjectInputStream in = new ObjectInputStream(new FileInputStream ("Eventos.dat"));
            eventos=(ArrayList<Evento>) in.readObject();
            in.close();
            return eventos;

        }catch(IOException ex){
            System.out.println("Ficheiro não existe");
            return null;
        }
    }
}

here is the Evento class:

public class Evento implements Serializable {

    private String titulo = "Nao preenchido";
    private String data = "Nao preenchido";
    private String local = "Nao preenchido";
    private String descricao = "Nao preenchido";
    private String privado = "Nao preenchido";
    private ArrayList<Contacto> convidados = new ArrayList();

   public Evento() {

    }

    public Evento(String titulo, String data, String local, String descricao, String privado) {
        this.titulo = titulo;
        this.data = data;
        this.local = local;
        this.descricao = descricao;
        this.privado = privado;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public void setData(String data) {
        this.data = data;
    }

    public void setLocal(String local) {
        this.local = local;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public void setPrivado(String privado) {
        this.privado = privado;
    }


    public String getTitulo() {
        return titulo;
    }

    public String getData() {
        return data;
    }

    public String getLocal() {
        return local;
    }

    public String getDescricao() {
        return descricao;
    }

    public String getPrivado() {
        return privado;
    }

    public ArrayList<Contacto> getConvidados() {
        return convidados;
    }

    public void setConvidados(ArrayList<Contacto> convidados) {
        this.convidados = convidados;
    }

    public String adicionaConvidado(String nomeConvidado){
        Contacto novo = new Contacto();
        for (Contacto contacto : this.convidados) {
             if(nomeConvidado.equals(contacto.getNome())){
                return "Contacto já foi convidado";
            }
         }
        novo.setNome(nomeConvidado);
        novo.setEmail("");
        novo.setTelefone("");
        convidados.add(novo);
        return "ADICIONEI CONVIDADO";

    }

    public Evento(String titulo, String local) {
        this.titulo = titulo;
        this.local = local;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Evento other = (Evento) obj;
        if (!Objects.equals(this.titulo, other.titulo)) {
            return false;
        }
        if (!Objects.equals(this.data, other.data)) {
            return false;
        }
        if (!Objects.equals(this.local, other.local)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Evento{" + "titulo=" + titulo + ", data=" + data + ", local=" + local + ", descricao=" + descricao + ", privado=" + privado + ", convidados=" + convidados + '}';
    }

    @Override
    public int hashCode() {
        int hash = 7;
        return hash;
    }

Changed the method CarregaEvento to:

public ArrayList<Evento> carregaEventos() throws ClassNotFoundException {

    try{

        ObjectInputStream in = new ObjectInputStream(new FileInputStream ("Eventos.dat"));
        eventos=(ArrayList<Evento>) in.readObject();
        in.close();
        return eventos;

    }catch(IOException ex){
        System.out.println("Ficheiro não existe");
        return null;
    }           

}

No errors but still doesn't work.

eventos=(ArrayList<Evento>) in.readObject();

This will not create a new type of an ArrayList<Evento> .

although you can create a new instance of an Evento with the String that is provided when you read the text file. you can use the split(String par1) method in the String class to create a new instance of Evento and add it to an arraylist.

refer to the JavaDocs for more info on splitting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM