Site hosted by Angelfire.com: Build your free website today!

import java.lang.*;

import java.lang.Object;

import java.io.*;

import Misclases.*;

 

class persona implements Serializable

{

String nom;

String apellido;

String dir;

String tel;

persona(String n,String a,String d,String t)

{

nom=n;

apellido=a;

dir=d;

tel=t;

}

}

class pro33

{

public static void main(String [] args)

{

int op,i=0;

String nom,nom1,dir,tel,ap;

FileInputStream fis = null;

ObjectInputStream dis = null;

ObjectOutputStream dos = null;

FileOutputStream fos = null;

persona per1= new persona("nada","nada","nada","nada");

persona arr[] =new persona[100];

try

{

while(true)

{

System.out.println("\t\t\tMenu");

System.out.println("\t\t1- Agregar");

System.out.println("\t\t2- Bucar");

System.out.println("\t\t3- Salir");

System.out.print("\t\t\tTeclea opcion: ");

op=Getdat.getInt();

if(op==3)

break;

if(op==1)

{

File archivo = new File("c:\\agenda.txt");

if(archivo.exists())

{

//archivo = new File(nom);

// archivo.listFiles();

// i=1;

fis=new FileInputStream("c:\\agenda.txt");

dis=new ObjectInputStream(fis);

try

{

while(true)

{

per1=(persona)dis.readObject();

arr[i]=per1;

i++;

}

}

catch(ClassCastException e)

{

System.out.println("Error");

}

catch(IOException e)

{

System.out.println("");

}

try

{

dis.close();

fis.close();

}

catch(IOException e)

{

System.out.println("Error2");

}

}

fos=new FileOutputStream("c:\\agenda.txt");

dos=new ObjectOutputStream(fos);

System.out.print("\n Nombre: ");

nom=Getdat.getCad();

System.out.print("\n Apellidos: ");

ap=Getdat.getCad();

System.out.print("\n Direccion: ");

dir=Getdat.getCad();

System.out.print("\n Telefono: ");

tel=Getdat.getCad();

persona per= new persona(nom,ap,dir,tel);

if(i==0)

dos.writeObject(per);

if(i!=0)

{

for(int j=0;j<i;j++){

per1=arr[j];

dos.writeObject(per1);}

dos.writeObject(per);

}

try

{

dos.close();

fos.close();

}

catch(IOException e)

{

System.out.println("Error1");

}

i=0;

}

if(op==2)

{

File archivo = new File("c:\\agenda.txt");

if(archivo.exists())

{

fis=new FileInputStream("c:\\agenda.txt");

dis=new ObjectInputStream(fis);

try

{

System.out.print("\n Introduce nombre a buscar: ");

nom1=Getdat.getCad();

while(true)

{

per1=(persona)dis.readObject();

if(per1.nom.equals(nom1)==true)

{

System.out.println(per1.nom);

System.out.println(per1.apellido);

System.out.println(per1.dir);

System.out.println(per1.tel);

System.out.println("");

}

}

}

catch(IOException e)

{

System.out.println("");

}

try

{

dis.close();

fis.close();

}

catch(IOException e)

{

System.out.println("Error2");

}

}

else

System.out.println("Archivo no existe");

}

}

}

catch (ClassNotFoundException e)

{

System.out.println("ARCHIVO12 NO EXISTE");

}

catch (FileNotFoundException e)

{

System.out.println("ARCHIVO13 NO EXISTE");

}

catch(IOException e)

{

System.out.println("ERROR3");

}

finally

{

try

{

if(fis!=null)

fis.close();

if(fos!=null)

fos.close();

if(dis!=null)

dis.close();

if(dos!=null)

dos.close();

}

catch(IOException e)

{

System.out.println("Error al cerrar archivos");

}

}

}

}