Java - Ayuda Archivo bin!

 
Vista:

Ayuda Archivo bin!

Publicado por Nets! (36 intervenciones) el 04/01/2016 21:16:12
Hola a todos necesito ayuda!
He echo el siguiente código: ( Puede que haya una forma más correcta de hacerlo.. pero este no es mi problema )

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package activitat2;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;
/**
 * ACTIVITAT 2 EAC 6
 * @author Net
 */
public class EquipIns{
public static void main(String[] args) {
        // TODO code application logic here
        EquipIns programa = new EquipIns();
        programa.inici();
    }
    public void inici(){
        System.out.println("Institutos....");
        System.out.println("1-Registrar los datos de los institutos ");
        System.out.println("2-Listar todos los institutos");
        System.out.println("3- Salir");
        System.out.println("Escoje una opción");
        Scanner lector = new Scanner(System.in);
        int punts=0;
        int enterLlegit =0;
        boolean llegit = false;
        while(!llegit){
            llegit = lector.hasNextInt();
            if (llegit){
                enterLlegit = lector.nextInt();
                switch (enterLlegit) {
        case 1:
            System.out.println("Registra los datos de los institutos:");
            System.out.println("Quantos institutos participan en el concurso?");
            boolean leido=false;
            int enterLlegit2 =0;
                while(!leido){
            leido = lector.hasNextInt();
            if (leido){
                enterLlegit2 = lector.nextInt();
            System.out.println("Numero total de centros: "+enterLlegit2);
            for (int j = 0; j < enterLlegit2; j++) {
            int i=0;
            i = enterLlegit2;
            if(enterLlegit2>0){
                System.out.println("-------------------------------------");
                System.out.println("Introduce la info sobre el instituto:");
                System.out.println("-------------------------------------");
                System.out.println("Id?");
                int idInst = lector.nextInt();
                System.out.println("Nom Institut:");
                String nomInst = lector.next();
                System.out.println("Nom Equip:");
                String nomEqui = lector.next();
                System.out.println();
 
                System.out.println("------------------------------------------");
                System.out.println("::INFORMACIÓN SOBRE CENTRO INSCRITO::");
                System.out.println("------------------------------------------");
                System.out.println("Id: " +idInst);
                System.out.println("Nom Institut: "+nomInst);
                System.out.println("Equip: "+nomEqui);
                System.out.println("Punts: "+punts);
                System.out.println();
                }
            }
 
              }else{
                System.out.println("No has escrito un entero, vuelve a intentarlo");
                lector.next();
            }
                }
 
   break;
        case 2:
            System.out.println("has escogido listar los institutos");
 
   break;
        case 3:
System.out.println("Hasta la próxima!");
        break;
        default:
System.out.println("No has escrito una opción válida");
System.out.println("Fin del programa");
        break;}
 
            }else{
                System.out.println("No has escrito un entero, vuelve a intentarlo");
                lector.next();
            }
        }
        lector.nextLine();
 
 
 }
}
Aquí viene mi duda:

Necesito apartir de este archivo Crear un fichero orientado a Byte.
Donde almacenar los datos introducidos por el usuario.

Me pone en el ejercicio que tengo que intruducir los registros con medida fija. Pone que de 20.
Máximo 48 bytes ( id ( 4 ) , Instituto (20), NombreEquipo ( 20 ) Puntos (4) = 48.
usando el método void Seek.


Es decir una vez introducidos los datos por el usuario estos deben de grabarse en el archivo BIN.

Luego en el Swich case 2 ( Mostrar esta información )


Como no tengo ejemplos no sé como hacerlo. Me iría genial verlo echo para poder seguir adelante en el proyecto que tengo.. :(

gracias !!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Ayuda Archivo bin!

Publicado por nets (36 intervenciones) el 05/01/2016 18:39:01
Lo he medio solucionado:

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package activitat2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * ACTIVITAT 2 EAC 6
 * @author Montse
 */
public class EquipIns{
public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        EquipIns programa = new EquipIns();
        programa.inici();
    }
    public void inici() throws FileNotFoundException, IOException{
         RandomAccessFile raf;
        raf = new RandomAccessFile("Resultat.bin", "rw");
        System.out.println("Institutos....");
        System.out.println("1-Registrar los datos de los institutos ");
        System.out.println("2-Listar todos los institutos");
        System.out.println("3- Salir");
        System.out.println("Escoje una opción");
        Scanner lector = new Scanner(System.in);
        int punts=0;
        int enterLlegit =0;
        boolean llegit = false;
        while(!llegit){
            llegit = lector.hasNextInt();
            if (llegit){
                enterLlegit = lector.nextInt();
                switch (enterLlegit) {
        case 1:
            System.out.println("Registra los datos de los institutos:");
            System.out.println("Quantos institutos participan en el concurso?");
            boolean leido=false;
            int enterLlegit2 =0;
                while(!leido){
            leido = lector.hasNextInt();
            if (leido){
                enterLlegit2 = lector.nextInt();
            System.out.println("Numero total de centros: "+enterLlegit2);
            for (int j = 0; j < enterLlegit2; j++) {
            int i=0;
            i = enterLlegit2;
            if(enterLlegit2>0){
                System.out.println("-------------------------------------");
                System.out.println("Introduce la info sobre el instituto:");
                System.out.println("-------------------------------------");
                System.out.println("Id?");
                int idInst = lector.nextInt();
                System.out.println("Nom Institut:");
                String nomInst = lector.next();
                System.out.println("Nom Equip:");
                String nomEqui = lector.next();
                System.out.println();
 
                System.out.println("------------------------------------------");
                System.out.println("::INFORMACIÓN SOBRE CENTRO INSCRITO::");
                System.out.println("------------------------------------------");
                System.out.println("Id: " +idInst);
                System.out.println("Nom Institut: "+nomInst);
                System.out.println("Equip: "+nomEqui);
                System.out.println("Punts: "+punts);
                System.out.println();
 
                 raf.seek(0);
                 raf.writeInt(idInst);
                 raf.seek(4);
                 raf.writeUTF(nomInst);
                 raf.seek(24);
                 raf.writeUTF(nomEqui);
                 raf.seek(44);
                 raf.writeInt(punts);
 
                }
            }
 
              }else{
                System.out.println("No has escrito un entero, vuelve a intentarlo");
                lector.next();
            }
                }
 
   break;
        case 2:
            System.out.println("has escogido listar los institutos");
             raf.seek(0);
    System.out.println(raf.readInt());
    raf.seek(4);
    System.out.println(raf.readUTF());
    raf.seek(24);
    System.out.println(raf.readUTF());
    raf.seek(44);
    System.out.println(raf.readInt());
 
    raf.close();
 
 
   break;
        case 3:
System.out.println("Hasta la próxima!");
        break;
        default:
System.out.println("No has escrito una opción válida");
System.out.println("Fin del programa");
        break;}
 
            }else{
                System.out.println("No has escrito un entero, vuelve a intentarlo");
                lector.next();
            }
        }
        lector.nextLine();
 
 
 }
}

El problema que tengo ahora es que los datos me los coge de las variables pero claro cuando escribo encima me borra el dato anterior... Como hago para tener la misma variable y que no se me borre con un array? :S

Si alguien puede hacerme un ejemplo.. T_T
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar