Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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 {
RegistroPersona registro;
}
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());
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;
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");