Problem storing java object on file

2

When I try to store an object in a binary file, the file with the .bin extension is created. However, when I try to access it by another class, it does not even try to open the file .bin that I recorded my object. How do I solve the problem?

Main Class

package ordenacao;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class Principal {

    public static void main(String[] args){
        lista l = new lista();
        lista aux = new lista();
        Scanner input = new Scanner(System.in);
        int x,i;

        for(i=0;i<4;i++){
            System.out.println("Digite um valor para empilhar:");
            x = input.nextInt();
            l.InsereInicio(x);
        }
        System.out.println("Dados Inseridos:");
        for(i=0;i<4;i++){
            x = l.removeInicio();
            aux.InsereFinal(x);
            System.out.println(x);
        }
        l = aux;
        try{
            OutputStream fileStream = new FileOutputStream("lista.bin");
            ObjectOutputStream os = new ObjectOutputStream(fileStream);
            os.writeObject(l);
            os.close();
            fileStream.close();
        }catch(IOException e){
            System.out.println("Erro 1");
        }
    }
}

Object I Want to Store

package ordenacao;

import java.io.Serializable;

public class lista implements Serializable{
    elemento primeiro;
    elemento ultimo;

    public void InsereInicio(int valor){
        elemento novoprimeiro = new elemento();
        novoprimeiro.dado = valor;
        novoprimeiro.proximo = primeiro;
        primeiro = novoprimeiro;
        if(ultimo==null){
            ultimo = novoprimeiro;
        }
    }

    public void InsereFinal(int valor){
        elemento novoultimo = new elemento();
        novoultimo.dado = valor;
        novoultimo.proximo = null;
        if(primeiro == null){
            primeiro = novoultimo;
        }else{
            ultimo.proximo = novoultimo;
        }
        ultimo = novoultimo;
    }

    int estaVazio(){
        if(primeiro==null){
            return 1;
        }
        return 0;
    }

    int removeInicio(){
        if(estaVazio()==1){
            System.out.println("Esta vazio");
            return -1;
        }
        int k = primeiro.dado;
        primeiro = primeiro.proximo;
        if(primeiro==null){
            ultimo = null;
        }
        return k;
    }

    int removeFinal(){
        if(estaVazio()==1){
            return -1;
        }
        int k = ultimo.dado;
        elemento fim = primeiro;
        elemento penultimo = null;
        while(fim.proximo!=null){
            penultimo = fim;
            fim = fim.proximo;
        }
        if(penultimo!=null){
            penultimo.proximo = null;
            ultimo = penultimo;
        }else{
            primeiro = null;
            ultimo = null;
        }
        return k;
    }

    int remover(int pos){
        if((pos<0)||(estaVazio()==1)){
            return -1;
        }
        elemento anterior = new elemento();
        elemento atual = primeiro;
        int i;
        for(i=0;i<pos-1;i++){
            anterior = atual;
            atual = atual.proximo;
        }
        anterior.proximo = atual.proximo;
        atual = null;
        return 1;
    }
}

Reader should read data

package pacotejava;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class PrincipalPacotenovo {
    public static void main(String[] args){
            int i;
            lista noval;
        try{
            InputStream conectar = new FileInputStream("lista.bin");
            ObjectInputStream os = new ObjectInputStream(conectar);
            Object obj = os.readObject();
            noval = (lista)obj;
            for(i=0;i<4;i++){
                System.out.println(noval.removeInicio());
            }
        }catch(IOException e){
            System.out.println("Erro 1");
        }catch(ClassNotFoundException e){
            System.out.println("Erro 2");
        }       
    }
}

------- Update -------

I was able to solve one of the problems when I throw throws IOException the compiler eventually showed me the lines that were causing error (I had not closed the reader and I had forgotten to implement in one of the classes of my Serializable package, and so I managed to compile the writer without problems), in relation to the reader I did not get the same luck, the error that appeared was the following:

Exception in thread "main" java.lang.ClassCastException: ordenacao.lista cannot be cast to pacotejava.lista
    at pacotejava.PrincipalPacotenovo.main(PrincipalPacotenovo.java:14)
C:\Users\Renan\AppData\Local\NetBeans\Cache.1\executor-snippets\run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)
    
asked by anonymous 30.03.2016 / 01:20

1 answer

0

Thanks for trying to help me, I realized the error after a while thinking, my idea was to try to write to the file my object, the idea of writing to the file was that it could convert the data of the object into a serialized bitstream and then reading it returns all the data stored in that object that was written to the file, the problem is that my object is composed of "pointers", and when I recorded the list object, it recorded not only the data but also the " pointers "from the other objects that were in the list, and the problem was there, the Java" pointer "is referenced by the package in which the object (java file) is located, and I was trying to reference it by a different package, and trying to do this it was giving error because it could not get the reference, did not think that the reference of the "pointer" would depend on the package too.

    
30.03.2016 / 07:51