Hola buenas, tengo que hacer 2 clases, una para leer unos datos y meterlos en un .dat y otro que mostrara esta informacion, no se por que no se me posiciona bien al mostrar los datos, creo que he contado bien los byts y todo pero no me lo muestra bien... alguien ve el error?
CLASE PARA ESCRIBIR
Código HTML:
import java.io.*;
public class Ex3
{
//Escriure
public static void main(String[] args) throws IOException
{
File fil = new File("empleat.dat");
RandomAccessFile file = new RandomAccessFile(fil, "rw");
//Arrays de cognoms, salari
String apellido[] = {"Reimundo", "Umelos", "Josum"};
int departament[] = {1, 2, 3};
Double sou[] = {1000.45, 3200.98, 9882.72};
StringBuffer buffer = null;
for(int i=0; i<apellido.length; i++)
{
file.write(i+1);//usem i+1 per identificar el usuari
buffer = new StringBuffer(apellido[i]);
buffer.setLength(10);
file.writeChars(buffer.toString());
file.writeInt(departament[i]);
file.writeDouble(sou[i]);
}
file.close();
}
}
CLASE PARA LEER
Código HTML:
import java.io.*;
import java.util.*;
public class Ex3_1
{
//Llegir
public static void main(String[] args) throws IOException
{
File arxiu = new File("empleat.dat");
RandomAccessFile file = new RandomAccessFile(arxiu, "rw");
//variables
int id, departament, posicion;
Double sou;
char apellido[] = new char[10], aux;
//Creem l'escaner
Scanner scan = new Scanner(System.in);
//Demenem la ID
System.out.println("Instrodueix la ID d'un empleat: ");
int identificador;
identificador = scan.nextInt();
//Demenem la cantitat a sumar
System.out.println("Instrodueix l'import a sumar: ");
Double importe;
importe = scan.nextDouble();
posicion = (identificador - 1)*36;
if(posicion >= file.length())
{
System.out.println("Aquest empleat no esta");
}
else
{
file.seek(posicion);
id = file.readInt();
for(int i=0; i<apellido.length; i++)
{
aux=file.readChar();
apellido[i]=aux;
System.out.print(apellido[i]);
}
String apellidoS = new String(apellido);
departament = file.readInt();
sou = file.readDouble();
importe = sou + importe;
posicion = posicion + 4 + 20 + 4;
file.seek(posicion);
file.writeDouble(importe);
System.out.println("Empleat: "+apellidoS);
System.out.println("Salari anterior: "+sou);
System.out.println("Salari actualizat: "+importe);
}
scan.close();
file.close();
}
}