You are on page 1of 6

Archivos Aleatorios public class ManejadorArchivoContactos { // manejador de un objeto private RandomAccessFile file; // Fichero de acceso aleatorio

public boolean abrir(String nombre) { // Apertura del fichero try { file = new RandomAccessFile(nombre, "rw"); return true; } catch (IOException e) { System.err.println("Error al leer del archivo"); return false; } } public void cerrar() { // Cierre del fichero try { if (file != null) { file.close(); } } catch (IOException e) { System.err.println("Error al leer del archivo"); } } public void escribir(RegistroContacto registro) { // Escribir un registro en la posicion actual del cursor if (file != null) { registro.escribirRegistro(file); } }
1

public void escribir(RegistroContacto registro, int pos) { // Escribir un registro en una posicin cualquiera try { if (file != null) { file.seek((pos - 1) * RegistroContacto.DIM); escribir(registro); } } catch (IOException e) { System.err.println("Error al leer del archivo"); } }

public RegistroContacto leer() {// Leer del fichero el registro la posicion actual del cursor RegistroContacto registro = null; if (file != null) { registro = new RegistroContacto(); registro.leerRegistro(file); } return registro; } public RegistroContacto leer(int pos) { // Leer del fichero un registro cualquiera try { // (el parametro indica la posicion del registro)

if (file != null) { file.seek((pos - 1) * RegistroContacto.DIM); } } catch (IOException e) { System.err.println("Error al leer del archivo"); } return leer(); } }
2

Registro del objetos public class RegistroContacto extends Contacto {

public final static int DIM = 172;

public RegistroContacto() { super(); }

public RegistroContacto(String nombre, String telefono, String email, int grupo, double deuda) { super(nombre, telefono, email, grupo, deuda); }

public void leerRegistro(RandomAccessFile file) { try { setNombre(leerString(file, 32)); setTelefono(leerString(file, 16)); setEmail(leerString(file, 32)); setGrupo(file.readInt()); //4 setDeuda(file.readDouble());//8 } catch (IOException e) { System.err.println("Error al leer del escribir"); } }

private String leerString(RandomAccessFile file, int tam) { char campo[] = new char[tam]; try { for (int i = 0; i < tam; i++) { campo[i] = file.readChar(); } } catch (IOException e) { System.err.println("Error al leer del archivo"); } String hilera = new String(campo); hilera = hilera.trim(); return hilera; }

public void escribirRegistro(RandomAccessFile file) { try { escribirString(file, getNombre(), 32); escribirString(file, getTelefono(), 16); escribirString(file, getEmail(), 32); file.writeInt(getGrupo()); file.writeDouble(getDeuda()); } catch (IOException e) { System.err.println("Error al escribir en el archivo"); } }

private void escribirString(RandomAccessFile file, String str, int dim) { StringBuilder buffer = new StringBuilder(); if (str != null) { buffer.append(str); } buffer.setLength(dim); try { file.writeChars(buffer.toString()); //Escribe al archivo } catch (IOException e) { System.err.println("Error al escribir en el archivo"); } } }

Prueba public class PruebaRandomContacto { public static void main(String args[]) throws IOException { ManejadorArchivoContactos agenda = new ManejadorArchivoContactos(); String nombre= "Alejandra Morales";

RegistroContacto contacto1 = new RegistroContacto(nombre, "2434-2382", "aulatec@gmail.com", 1, 4.5); RegistroContacto contacto2 = new RegistroContacto("Jose Garcia", "2566-7777", "pedro@gmail.com", 2, 7.8);

RegistroContacto obtener;

agenda.abrir("clientes.Txt"); agenda.escribir(contacto1); agenda.escribir(contacto2); obtener = agenda.leer(1); System.out.println(obtener.toString()); obtener = agenda.leer(2); System.out.println(obtener.toString()); agenda.cerrar(); } }

You might also like