Está en la página 1de 7

REGISTRO PERSONA CLASS

public class RegistroPersona {


private String nombre;
private String apellido;
private String cedula;
private int edad;

public RegistroPersona(){
nombre="";
apellido="";
cedula="";
edad=0;
}
public RegistroPersona(String nom, String ap, String ci, int e){
nombre=nom;
apellido=ap;
cedula=ci;
edad=e;
}
Archivo Texto
public class ArchivoTexto {

private Scanner entrada;

private Formatter salida; //Objeto usado para enviar texto al


archivo

public void abrirArchivoEscritura() {


try {
salida = new Formatter("c:\\prueba.txt");
}
catch (SecurityException securityException) {
System.err.println("No tiene acceso de escritura a
este archivo");
System.exit(1);
}
catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error al crear archivo");
System.exit(1);
}
}
Archivo Texto

public void agregarRegistros(){

RegistroPersona registro;

registro = new RegistroPersona("Pedro","Garcia","13552878",67);


salida.format("%s %s %s %d\n", registro.getNombre(),
registro.getApellido(), registro.getCedula(), registro.getEdad());

}
Archivo de Texto
public void abrirArchivoLectura() {
try {
entrada = new Scanner(new File("c:\\prueba.txt"));
}catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error al abrir archivo");
System.exit(1);
}
}
//Objeto que se escribe en pantalla
RegistroPersona reg =new RegistroPersona(); Archivo de Texto
System.out.printf("%-15s%-15s%-10s%-4s\n", "APELLIDO", "NOMBRE", "CEDULA", "EDAD");

try{
while (entrada.hasNext()){
reg.setApellido(entrada.next());
reg.setNombre(entrada.next());
reg.setCedula(entrada.next());
reg.setEdad(entrada.nextInt());

//Muestra contenido de registro


System.out.printf("%-15s%-15s%-10s%-2s\n", reg.getApellido(), reg.getNombre(),
reg.getCedula(), reg.getEdad());
}
}catch (NoSuchElementException elementException) {
System.err.println("El archivo no esta bien formado");
entrada.close();
System.exit(1);
}
catch (IllegalStateException stateException) {
System.err.println("Error al leer archivo");
System.exit(1);
}
}
ublic class ArchivoSerializable {
Archivo Serializable
private ObjectOutputStream salida; //Envia datos a un archivo
private ObjectInputStream entrada;

//Permite especificar nombre del archivo


public void abrirArchivoEscritura() {
try {
salida = new ObjectOutputStream(new FileOutputStream("c:\\pruebaser.txt"));
} catch (IOException exception) {
System.err.println("Error al abrir el archivo");
System.exit(1);
}
}

public void agregarRegistros(){


RegistroPersonaSerializable registro; //Registro que se va a escribir en archivo

try {
registro = new RegistroPersonaSerializable("Pedro","Perez","12345678");
salida.writeObject(registro);
} catch (IOException ex) {
System.err.println("Error al escribir en archivo");
return;
}
Archivo Serializable
public void leerRegistros(){
RegistroPersonaSerializable registro;

System.out.printf("%-15s%-15s%-10s\n", "APELLIDO", "NOMBRE", "CEDULA");

try {

while(true){

registro=(RegistroPersonaSerializable) entrada.readObject();
System.out.printf("%-15s%-15s%-10s\n", registro.getApellido(),
registro.getNombre(), registro.getCedula());
//System.out.printf("%s %s %s \n", regS.getApellido(), regS.getNombre(),
regS.getCedula());}
}
} catch (EOFException endOfFileException) {
return;//Se llego al fin de archivo
} catch (ClassNotFoundException ex) {
System.err.println("No se pudo crear el objeto");
}
catch(IOException ioException){
System.err.println("Error al leer archivo");

También podría gustarte