Código:
import java.lang.*;
import java.io.*;
import java.util.*;
public class Random {
private static RandomAccessFile archivo;
public static int Abrir(){
try {
archivo = new RandomAccessFile("Codigo804.dat", "rw");
return 0;
} catch(FileNotFoundException e) {
System.out.println("No existe archivo");
return 1;
} catch(IOException e) {
System.out.println("IO Exception");
return 1;
}
}
private static boolean Escribe(int numero,String nombre, String email, long tel) {
try {
archivo.writeInt(numero);
archivo.writeUTF(nombre);
archivo.writeUTF(email);
archivo.writeLong(tel);
return true;
} catch(IOException e) {
System.out.println("IO Exception");
return false;
}
} //Escribe Archivo
private static void Imprime(int n, String nomb,String em, long tel) {
System.out.println(n);
System.out.println(nomb);
System.out.println(em);
System.out.println(tel);
} //Imprime
private static void Agregar() {
try {
/*Se agrega al final del archivo*/
archivo.seek(archivo.length());
/*Numero de control del alumno*/
int numeroControl =Datos.getEntero("Número de control");
if(numeroControl > 0){
String nombreAlumno =
Datos.getCadena("Nombre del alumno", 30);
String email =Datos.getCadena("Correo electrónico", 40);
long telefono =Datos.getLargo("Número de teléfono");
Escribe(numeroControl, nombreAlumno,email, telefono);
}
} catch(IOException e) {
System.out.println("IO Exception");
}
}//Agregar
private static void Buscar() throws IOException {
int numeroControl = 0, nc = 0;
long telefono = 0;
String nombreAlumno = "", email = "";
try {
numeroControl = Datos.getEntero("Número de Control");
if(numeroControl > 0) {
archivo.seek(0);
while(numeroControl != nc){
nc = archivo.readInt();
nombreAlumno = archivo.readUTF();
email = archivo.readUTF();
telefono = archivo.readLong();
archivo.seek(archivo.getFilePointer());
}//while
Imprime(nc, nombreAlumno,email, telefono);
}
} catch (EOFException e) {
/* Se genera una excepción al encontrar el fin de archivo*/
System.out.println("No se encontró el númerode control: " + numeroControl);
}
}//Mostrar
private static void Mostrar() throws IOException {
try {
archivo.seek(0);
while(true){
Imprime(archivo.readInt(), archivo.readUTF(),archivo.readUTF(),archivo.readLong());
System.out.println();
archivo.seek(archivo.getFilePointer());
}//while
} catch (EOFException e) {
/* Se genera una excepción al encontrar el fin de archivo*/
System.out.println("Fin de archivo");
}
}//Mostrar
private static void Editar() throws IOException {
int numeroControl = 0, nc = 0;
long posicion, telefono = 0, tel;
String S, nombreAlumno = "", email = "";
try {
numeroControl =
Datos.getEntero("Número de Control");
if(numeroControl > 0) {
archivo.seek(0);
do {
posicion = archivo.getFilePointer();
archivo.seek(posicion);
nc = archivo.readInt();
nombreAlumno = archivo.readUTF();
email = archivo.readUTF();
telefono = archivo.readLong();
}while(numeroControl != nc);
if(numeroControl == nc) {
archivo.seek(posicion);
nc = archivo.readInt();
nombreAlumno = archivo.readUTF();
email = archivo.readUTF();
telefono = archivo.readLong();
Imprime(nc, nombreAlumno,email, telefono);
archivo.seek(posicion);
S = Datos.getCadena("Nombre ", 30);
nombreAlumno = (S.length() > 0)? S: nombreAlumno;
S = Datos.getCadena("Correo", 40);
email = (S.length() > 0)? S: email;
tel = Datos.getLargo("telefono");
telefono = (tel > 0) ? tel: telefono;
Escribe(numeroControl, nombreAlumno,email, telefono);
}
}
} catch (EOFException e) {
/* Se genera una excepcion al encontrar el fin de archivo*/
System.out.println("No se encontró el númerode control: " + numeroControl);
}
}//Editar
public static void main(String[] args) throws IOException {
int opcion = 5;
if(Abrir()== 0) do {
System.out.println("\n\t1)Agregar\n\t2)Buscar\n\t3)Editar\n\t4)Mostrar\n\t5)Salir");
opcion = Datos.getEntero("Opción");
switch(opcion){
case 1: Agregar();
break;
case 2: Buscar();
break;
case 3: Editar();
break;
case 4: Mostrar();
break;
} //switch
} while(opcion != 5);
archivo.close();
System.out.println("Bye");
}//main
}


