Java - Ejercicios resueltos de Ficheros de acceso aleatorio

 
Vista:

Ejercicios resueltos de Ficheros de acceso aleatorio

Publicado por burlon (14 intervenciones) el 08/06/2015 22:33:51
Utilizando un fichero aleatorio, realiza un programa que le muestre al usuario un menú con las siguientes opciones:
• 1.‐ Añadir números de tipo double al principio del fichero.
• 2.‐ Añadir números de tipo double al final del fichero.
• 3.‐ Mostrar el fichero creado.
• 4.‐ Sustituir un número indicado por el usuario por otro número que también te indique el usuario.
• Nota: Un double en JAVA ocupa 8 bytes.

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.io.File;
import java.io.RandomAccessFile;
import java.util.Random;
import java.util.Scanner;
 
 
public class Aleatorio {
 
    public static void menu(){
        Scanner teclado = new Scanner(System.in);
        int opcion;
        do {
 
            System.out.println("1.-Aniadir numeros de tipo double al principio del fichero");
            System.out.println("2.Aniadir numero de tipo double al final del fichero.");
            System.out.println("3. Mostrar el fichero creado.");
            System.out.println("4. Sustituir un número indicado por el usuario por otro numero que también te indique el usuario.");
            System.out.println("5.- Salir del programa");
            opcion = teclado.nextInt();
 
            switch (opcion) {
                case 1: {
                    crearFichero();
 
                    break;
                }
                case 2: {
                    crearFichero2();
 
                    break;
                }
                case 3: {
                    leerFichero();
                    break;
                }
                case 4: {
                    sustituirDoubles();
                    break;
                }
                case 5: {
                    System.out.println("Gracias por usar el programa....");
                    break;
                }
                default: {
                    System.out.println("Opcion no es correcta");
                }
            }
        } while (opcion != 5);
 
    }
 
    public static void crearFichero() {
        RandomAccessFile raf = null;
        try {
            File fichero = new File("fichero.obj");
            raf = new RandomAccessFile(fichero, "rw");
            añadirNumenosAlPrincipio(raf);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (raf != null) {
                    raf.close();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
 
    }
 
    public static void añadirNumenosAlPrincipio(RandomAccessFile raf) throws Exception {
        Scanner teclado = new Scanner(System.in);
        double numero;
        System.out.println("Introduce un numero Double para añadir al principio del fichero: ");
        numero = teclado.nextDouble();
        raf.seek(0);
        raf.writeDouble(numero);
 
    }
 
    public static void crearFichero2() {
        RandomAccessFile raf = null;
        try {
            File fichero = new File("fichero.obj");
            raf = new RandomAccessFile(fichero, "rw");
            añadirNumenosAlFinal(raf);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (raf != null) {
                    raf.close();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
 
    public static void añadirNumenosAlFinal(RandomAccessFile raf) throws Exception {
        Scanner teclado = new Scanner(System.in);
        double numero;
        System.out.println("Introduce un numero Double para añadir al principio del fichero: ");
        numero = teclado.nextDouble();
        raf.seek(raf.length());
        raf.writeDouble(numero);
 
    }
 
    public static void leerFichero() {
        RandomAccessFile raf = null;
        try {
            File fichero = new File("fichero.obj");
            raf = new RandomAccessFile(fichero, "r");
            mostrarFichero(raf);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (raf != null) {
                    raf.close();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
 
    public static void mostrarFichero(RandomAccessFile raf) throws Exception {
        double numero;
        raf.seek(0);
        while (true) {
            numero = raf.readDouble();
            System.out.println(numero);
        }
    }
 
    public static void sustituirDoubles() {
        Scanner teclado = new Scanner(System.in);
        double sustituir, nuevo, numero;
        int i = 0;
 
        RandomAccessFile raf = null;
 
        try {
            File fichero = new File("fichero.obj");
            raf = new RandomAccessFile(fichero, "rw");
            raf.seek(0);
 
            System.out.println("Introduce el valor que desea sustituir");
            sustituir = teclado.nextDouble();
 
            System.out.println("Introduce el nuevo valor");
            nuevo = teclado.nextDouble();
 
            while (true) {
                numero = raf.readDouble();
                if (numero == sustituir) {
                    System.out.println("Encontrado en la posicion " + i);
                    raf.seek(i * 8);
                    raf.writeDouble(nuevo);
                }
                i++;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (raf != null) {
                    raf.close();
                }
            } catch (Exception e2) {
                System.out.println(e2.getMessage());
            }
        }
 
    }
 
    public static void main(String[] args) {
        menu();
    }
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
1
Responder

Ejercicios resueltos de Ficheros de acceso aleatorio

Publicado por juan vera castillo (2 intervenciones) el 09/06/2018 22:47:58
Vale el esfuerzo, pero lamentablemente este ejercico practicamewnte sirve de muy poco, es extremadamente básico. Ideal sería un ejemplo con almacenamiento de objetos o registros. saludos
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