What I need is to know why when I enter a record to read I skip the EOF exception that says there are no more records in the file. I have created in the code that each record that has a length of 44 bytes to go directly to a record. Then I wanted to know what my fault is .. I tell him that I want to go to register 1 and I skip the exception. Can you make me an example file, or tell me where the fault is?
import java.io.*;
import java.util.Scanner;
class Principal{
public static void main(String [] args) {
int longRegistro, n, id;
double salario;
String apellido;
Scanner lectura = new Scanner (System.in);
RandomAccessFile entrada = null;
String nombreFichero;
try {
System.out.println("Introduzca el nombre del fichero");
nombreFichero = lectura.next();
entrada = new RandomAccessFile(nombreFichero, "r");
System.out.println("Introduzca el numero de registro a leer");
n = lectura.nextInt();
longRegistro = 44; //4 bytes (int), 30(String)+2 UTF, 8(double)
entrada.seek((n - 1) * longRegistro);
id = entrada.readInt();
apellido = entrada.readUTF();
salario = entrada.readDouble();
System.out.printf("%d %s %.2f\n", id, apellido, salario);
}
catch (EOFException e) {
System.out.println("\nHa llegado al final del fichero. " + "El numero de registro no existe.");
}
catch (FileNotFoundException e) {
System.out.println("El fichero especificado no existe");
}
catch(IOException e) {
System.out.println("Excepcion de entrada/salida:" +
e.toString());
System.out.println(e.getMessage());
}
finally {
try {
entrada.close();
}
catch (IOException io) {
System.out.println("No se ha podido cerrar el fichero" +io.toString());
System.out.println(io.getMessage());
}
} // Fin finally
} // Fin método main
}