How to write objects to a file and read these objects

1

I need to save the created objects to a TXT file and then read the recorded files and display the recorded objects. I researched in several places but I can not solve my problem.

I have a Client class already implementing Serializable .

package lista7Questao2;

import java.io.*;
import java.util.Scanner;

public class PrincipalClienteQuestao2 {

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        Scanner sc = new Scanner(System.in);
        String dia,
               mes,
               ano,
               sexo,
               nome = "";
        int age;

        Cliente c = new Cliente();

            try{

            FileOutputStream fileos=  new FileOutputStream("D:\System 32\POO\Lista 7\Cadastro Clientes.txt");
            ObjectOutputStream obj = new ObjectOutputStream(fileos);

                while (!(nome.equals("fim") || nome.equals("FIM"))){

                System.out.println("Nome: ");
                nome = sc.nextLine();
                if(nome.equals("fim") || nome.equals("FIM")){
                    System.out.println("Programa encerrado.");
                    break;
                }

                System.out.println("dia: ");
                dia = sc.nextLine();

                System.out.println("Mes: ");
                mes = sc.nextLine();

                System.out.println("Ano: ");
                ano = sc.nextLine();

                System.out.println("Sexo: ");
                sexo = sc.nextLine();

                //SETTANDO OS DADOS PARA AS VARIAVEIS
                c.setNome(nome);
                c.setDia(dia);
                c.setMes(mes);
                c.setAno(ano);
                c.setSexo(sexo);

                age = Integer.parseInt(ano);
                int idade = c.calculaIdade(age);

                //GRAVANDO O OBJETO CLIENTE NO ARQUIVO TXT
                obj.writeObject(c);
                //obj.writeObject(c.getDia());
                //obj.writeObject(c.getMes());
                //obj.writeObject(c.getAno());
                //obj.writeObject(c.getSexo());
                obj.flush();
                obj.close();

                FileInputStream is =  new FileInputStream("D:\System 32\POO\Lista 7\Cadastro Clientes.txt");
                ObjectInputStream ois = new ObjectInputStream(is);
                Cliente c1 = (Cliente) ois.readObject();
                ois.close();
                System.out.println(c1.toString());
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }finally{
                sc.close();
            }

            /*try{
                FileInputStream is =  new FileInputStream("D:\System 32\POO\Lista 7\Cadastro Clientes.txt");
                ObjectInputStream ois = new ObjectInputStream(is);

                //LEITURA DO OBJETO CLIENTE NO ARQUIVO
                Object c1 = ois.readObject();


                //System.out.println(c1.toString());

                ois.close();

            }*/
    }

}
    
asked by anonymous 29.05.2017 / 21:37

0 answers