EOFException Java and two more errors

0

I explain I am doing a program in java that inserts an Id, surname department and salary by arguments. In a .dat file, but first check that there is no person with the same id, finally read all the "employees" and show them by console. The despair is that it gives me three mistakes that I can not solve. I put the code below, I would use much what is wrong and how to solve it.

public class Programa3 {
public static void main(String[] args) throws IOException {
    try {
        int input_id = Integer.parseInt(args[0]);
        String input_apellido = args[1];
        int input_dep = Integer.parseInt(args[2]);
        Double input_salario = Double.parseDouble(args[3]);

        comprobarEmpleado(input_id,input_apellido,input_dep,input_salario);

        leer();

    }catch (ArrayIndexOutOfBoundsException ex) {
        System.out.println("Introduce bien los datos por argumentos/parametros");
    }

} //ok
public static void insertarEmpleado (int w_id, String w_apellido,int w_dep, Double w_salario ) throws IOException {
    File fichero = new File("src/AleatorioEmpleado.dat");
    RandomAccessFile file = new RandomAccessFile(fichero, "rw");

    int posicion = (int) file.length();
    file.seek(posicion);
    file.writeInt(w_id);
    file.writeChars(w_apellido);
    file.writeInt(w_dep);
    file.writeDouble(w_salario);

    file.close();

}

public static void comprobarEmpleado(int c_id, String c_apellido,int c_dep, Double c_salario) throws IOException{
    File fichero = new File ("src/AleatorioEmpleado.dat");
    RandomAccessFile file = new RandomAccessFile (fichero, "r");

    int id ,posicion;
    posicion = 0;

    for ( ; ; ) {
        file.seek (posicion);
        id = file.readInt(); 


        if (id == c_id) {
            System.out.printf("El empleado ya existe");
            break;

        }else {
            posicion = posicion + 36;
            System.out.println(posicion + " " +file.length());
            if (posicion == file.length()+4) {
                if (c_id > 0) {
                    insertarEmpleado(c_id,c_apellido,c_dep,c_salario);
                }else {
                    System.out.println("El id introducido no es valido");
                }
                break; 
            }
        }

    }

    file.close();
}

public static void leer() throws IOException{
    File fichero = new File ("src/AleatorioEmpleado.dat");
    RandomAccessFile file = new RandomAccessFile (fichero, "r");
    int id, dep ,posicion;
    Double salario;
    char apellido[]= new char[10], aux;
    posicion =0;

    for ( ; ; ){
        // Nos posicionamos en posicion
        file.seek (posicion); 

        // Obtengo identificar de Empleado
        id = file.readInt(); 

        for ( int i =0; i<apellido.length; i++) {
            // Voy leyendo carácter a carácter el apellido y lo guardo
            aux = file.readChar(); 

            // en el array apellido
            apellido[i]=aux;
        }
        String apellidos = new String (apellido);

        //Lectura de departamento y salario
        dep = file.readInt();
        salario = file.readDouble();

        if (id >0)
            System.out.printf("ID: %s, Apellido: %s, Departamento: %d, Salario: %.2f %n", id,
            apellidos.trim(), dep, salario);

        //Me posiciono para el siguiente empleado.
        //Cada uno ocupa 36 bytes
        posicion = posicion + 36; 

        //Si he recorrido todo el fichero saldo del for
        if (file.getFilePointer() == file.length()) break;
    }
    file.close();
}
}
    
asked by Caldeiro 20.12.2018 в 18:50
source

1 answer

0

My suggestion is to separate the code better to do the operations you need. To extract a plain text file you could use this algorithm

public Stream<String> leerAchivoExterno(String rutaArchivo) {
    Stream<String> lineas = null;

    try {
        lineas = Files.lines(Paths.get(rutaArchivo), Charset.forName("UTF-8"));
    } catch (IOException ex) {
        Logger.getLogger(ArchivoImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

    return lineas;
}

Once you have retrieved the .dat information, you can store the information in a class with matching attributes to the data you will always need.

public class Empleado {

private int id;
String apellido;
String departamento;
double salario;

public Empleado() {
}

public Empleado(int id, String apellido, String departamento, double salario) {
    this.id = id;
    this.apellido = apellido;
    this.departamento = departamento;
    this.salario = salario;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getApellido() {
    return apellido;
}

public void setApellido(String apellido) {
    this.apellido = apellido;
}

public String getDepartamento() {
    return departamento;
}

public void setDepartamento(String departamento) {
    this.departamento = departamento;
}

public double getSalario() {
    return salario;
}

public void setSalario(double salario) {
    this.salario = salario;
}

}

Subsequently, to write all the information of interest you can use this algorithm.

public void escribirArchivo(Path rutaDestino, List<String> lineas) {

    try {
         if(Files.exists(rutaDestino)){ // escribe al final
                Files.write(rutaDestino, lineas, Charset.forName("UTF-8"), StandardOpenOption.APPEND);
         }else{ // escribe todo el archivo
               Files.write(rutaDestino, lineas, Charset.forName("UTF-8"), StandardOpenOption.WRITE);
         }

    } catch (IOException ex) {
            Logger.getLogger(ArchivoImpl.class.getName()).log(Level.SEVERE, null, ex);
    }


}

In general lines you will need at least these operations and all those that you need to add. But get used to using a class as an entity to recover or map information structure of any system that looks like a database.

    
answered by 20.12.2018 в 21:53