Random Access File
Publicado por Joker (27 intervenciones) el 04/11/2021 15:59:19
Hola!
Estoy teniendo problemas y es que no sé porque no consigo que funcione, diría que es un problema con los bits pero no estoy seguro. Alguien que me pueda ayudar? Gracias!
Los datos que meto son así:
Y el codigo que tengo es el siguiente:
Estoy teniendo problemas y es que no sé porque no consigo que funcione, diría que es un problema con los bits pero no estoy seguro. Alguien que me pueda ayudar? Gracias!
Los datos que meto son así:
1
2
3
4
5
6
7
Id: Numero Entero.
DNI: String [9].
Nombre: String[10].
Identidad secreta: String[20].
Tipo: String[10]
Peso: Numero Entero.
Altura: Numero Entero
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
public static void main(String[] args) throws IOException {
//Longitud del registro
final int long_registro = 168;
try {
File fichero = new File(".\\src\\TAE02\\Marvel.dat");
RandomAccessFile file = new RandomAccessFile(fichero, "rw");
String dniFichero, nombreFichero, identidadFichero, tipoFichero, tipoConsola;
int pesoFichero, alturaFichero, i, p;
Boolean existeTipo = false;
System.out.print("Introduce el tipo de personaje:");
tipoConsola = Consola.readString();
for (p = 0; p < file.length(); p += long_registro) {
file.seek(p);
file.skipBytes(124);
//Se coge el tipo
char[] auxTipo = new char[10];
for (i = 0; i < 10; i++) {
auxTipo[i] = file.readChar();
}
tipoFichero = new String(auxTipo);
if (tipoFichero.trim().equalsIgnoreCase(tipoConsola)) {
existeTipo = true;
file.seek(p);
//Saltamos el ID
file.skipBytes(4);
//Se coge el DNI
char[] auxDNI = new char[9];
for (i = 0; i < 9; i++) {
auxDNI[i] = file.readChar();
}
dniFichero = new String(auxDNI).trim();
//Se coge el Nombre
char[] auxNombre = new char[10];
for (i = 0; i < 20; i++) {
auxNombre[i] = file.readChar();
}
nombreFichero = new String(auxNombre).trim();
//Se coge la identidad
char[] auxIdentidad = new char[20];
for (i = 0; i < 40; i++) {
auxIdentidad[i] = file.readChar();
}
identidadFichero = new String(auxIdentidad).trim();
//Salto el tipo
file.skipBytes(10);
//Se coge la peso
pesoFichero = file.readInt();
//Se coge la altura
alturaFichero = file.readInt();
//Se visualizan los datos del personaje
System.out.println("Pesonaje [Dni = " + dniFichero + ", Nombre = " + nombreFichero +
", Identidad = " + identidadFichero + ", Tipo = " + tipoFichero + ", Peso = " + pesoFichero +
", Altura = " + alturaFichero);
}
}
if (!existeTipo)
System.out.println("No existe el tipo " + tipoConsola + " en el fichero.");
file.close();
} catch (FileNotFoundException e) {
}
}
Valora esta pregunta


0