Java - Ejercicios resueltos de ficheros texto

 
Vista:

Ejercicios resueltos de ficheros texto

Publicado por burlon (14 intervenciones) el 08/06/2015 21:37:46
Realiza un programa en JAVA en el quemuestres un menu que te permita 3
opciones:
1. Creacion de un fichero de texto (con el nombre que tu quieras) en el que indiques en
cada linea:
Tu Nombre.
Tus Apellidos.
Tu Ciudad de Nacimiento.
2. Mostrar por pantalla el contenido del fichero de texto creado.
3. Salir del Programa.

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
public class Ejercicio01 {
 
    public static void mostrarMenu() {
        Scanner teclado = new Scanner(System.in);
        int opcion;
 
        do {
 
            System.out.println("1-.Crear Fichero");
            System.out.println("2-.Mostrar Fichero");
            System.out.println("3-.Salir");
 
            opcion = teclado.nextInt();
            switch (opcion) {
                case 1: {
                    crearFichero();
                    break;
                }
                case 2: {
                    mostrarFichero();
                    break;
                }
                case 3: {
                    System.out.println("Gracias por usar el programa");
                }
                default: {
                    System.out.println("Opcion incorrecta");
                }
 
            }
        } while (opcion != 3);
    }
 
    public static void crearFichero() {
        FileWriter fw = null;
        try {
            fw = new FileWriter("archivo.txt");
            PrintWriter pw = new PrintWriter(fw);
            escribirFichero(pw);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (fw != null) {
                    fw.close();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
 
    }
 
 
 
 
    public static void escribirFichero(PrintWriter pw) throws Exception {
        Scanner teclado = new Scanner(System.in);
        String opcion;
        System.out.println("Introduce tu nombre");
        opcion = teclado.nextLine();
        pw.println(opcion);
        System.out.println("Introduce tus apellidos");
        opcion = teclado.nextLine();
        pw.println(opcion);
        System.out.println("Introduce tu lugar de nacimiento");
        opcion = teclado.nextLine();
        pw.println(opcion);
    }
    public static void leerFichero(BufferedReader br) throws Exception {
        String linea;
        linea = br.readLine();
        while (linea != null) {
 
            System.out.println(linea);
            linea = br.readLine();
 
        }
    }
 
    public static void mostrarFichero() {
        FileReader fr = null;
        try {
            File fichero = new File("archivo.txt");
            fr = new FileReader(fichero);
            BufferedReader br = new BufferedReader(fr);
            leerFichero(br);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
 
    }
 
    public static void main(String[] args) {
        mostrarMenu();
    }
 
}


Realiza un programa en JAVA en el quemuestres un menú que te permita 3
opciones:
1. Volcado de un array con los 100 primeros números pares a un fichero de texto. El
nombre del fichero lo elegirá el usuario.
2. Mostrar por pantalla el contenido del fichero de texto creado.
3. Salir del Programa.

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
package ejercicio02tema08;
import java.io.*;
import java.util.Scanner;
 
 
public class Ejercicio02Tema08 {
    static String nombre;
 
    public static void mostrarMenu() {
        Scanner teclado = new Scanner(System.in);
        int opcion;
        do {
 
        System.out.println("1.- Crear fichero");
        System.out.println("2.- Mostrar Fichero");
        System.out.println("3.- Salir");
 
        opcion = teclado.nextInt();
 
        switch (opcion){
            case 1 : {
                crearFichero();
                break;
            }
            case 2 : {
                mostrarFichero();
                break;
            }
            case 3 : {
                System.out.println("Gracias por usar el Programa");
                break;
            }
            default:{
                System.out.println("Opcion no Valida");
            }
        }
 
        } while (opcion != 3);
    }
 
    public static String elegirNombre() {
        Scanner teclado = new Scanner(System.in);
 
 
        System.out.println("Introduce el nombre del Archivo");
        nombre = teclado.nextLine();
        return nombre;
 
    }
    public static String devolverNombre() {
        return nombre;
    }
 
    public static void crearFichero() {
 
        FileWriter fw = null;
        try {
            fw = new FileWriter(elegirNombre() + ".txt");
            PrintWriter pw = new PrintWriter(fw);
            escribirFicheo(pw);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally{
            try {
                if (fw != null) {
                    fw.close();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
    public static void escribirFicheo(PrintWriter pw) throws Exception{
        for (int i = 0; i < 100; i++) {
 
            if (i % 2 == 0 ) {
                pw.println(i);
            }
 
        }
    }
 
    public static void mostrarFichero() {
        FileReader fr = null;
        try {
            File fichero = new File(devolverNombre()+ ".txt");
            fr = new  FileReader(fichero);
            BufferedReader br = new BufferedReader(fr);
            leerFichero(br);
        } catch (Exception e) {
            System.out.println(e.getMessage());
 
        }finally{
 
                if (fr != null) {
                    try {
                        fr.close();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
 
                }
 
            }
 
    }
    public static void leerFichero(BufferedReader br)throws Exception{
        String linea;
        linea = br.readLine();
        while (linea != null) {
            System.out.println(linea);
            linea = br.readLine();
        }
    }
    public static void main(String[] args) {
        mostrarMenu();
    }
 
}


Crea un fichero de texto, en el Bloc de Notas, con el nombre y contenido
que tú quieras. Ahora crea una aplicación en JAVA que lea este fichero de
texto (¡OJO!  Carácter a carácter, no línea a línea) y muestre su
contenido por pantalla sin espacios.
• Por ejemplo, si un fichero contiene el texto “Esto es una prueba”, deberá mostrar por
pantalla “Estoesunaprueba”.

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
public class Ejercicio03tema08 {
 
    public static void mostrarFichero() {
        FileReader fr = null;
 
        try {
            File fichero = new File("pp.txt");
            fr = new FileReader(fichero);
 
            leerFichero(fr);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally{
            if (fr != null) {
                try {
                    fr.close();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }
    public static void leerFichero(FileReader fr) throws Exception{
        int letra;
        char caracter;
        letra = fr.read();
        while (letra != -1) {
            caracter=(char)letra;
            System.out.print(caracter);
            letra = fr.read();
        }
    }
    public static void main(String[] args) {
        mostrarFichero();
    }
 
}
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

Ejercicios resueltos de ficheros texto

Publicado por burlon (14 intervenciones) el 08/06/2015 21:57:59
Crea una aplicación donde pidamos el nombre de un fichero por teclado y un texto que queramos escribir en el fichero.
Después de crear el fichero con la información introducida, deberás mostrar por pantalla e texto del fichero pero variando entre mayúsculas y minúsculas.
• Por ejemplo, si escribo:
“Bienvenidos a Plasencia” deberá devolver
“bIENVENIDOS A pLASENCIA”.

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
public class Ejercicio04Tema08 {
 
   static String nombreFichero;
   static String testo;
 
    public static String solicitarFichero() {
        Scanner teclado = new Scanner(System.in);
 
        System.out.println("Introduce el nombre del fichero");
        nombreFichero = teclado.nextLine();
        return  nombreFichero;
    }
    public static String devolverFichero() {
        return  nombreFichero;
 
    }
    public static String solicitarTesto() {
        Scanner teclado = new Scanner(System.in);
 
        System.out.println("Introduce el Testo del fichero");
        testo = teclado.nextLine();
        return testo;
    }
 
    public static String devolverTesto() {
        return testo;
 
    }
 
    public static void crearFichero() {
        FileWriter fw = null;
        try {
            fw = new FileWriter(solicitarFichero() + ".txt");
            PrintWriter pw = new PrintWriter(fw);
            escribirFichero(pw);
 
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally{
            try {
                if (fw != null) {
                fw.close();
            }
 
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
 
        }
    }
    public static void escribirFichero(PrintWriter pw) throws Exception{
        pw.println(solicitarTesto());
 
    }
 
    public static void mostrarFichero() {
        FileReader fr = null;
        try {
            File fichero = new File(devolverFichero() + ".txt");
            fr = new FileReader(fichero);
            BufferedReader br = new BufferedReader(fr);
            leerFichero(br);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
 
    public static void leerFichero(BufferedReader br)throws Exception{
 
    }
    public static void invertirCadena(String cadena, char [] cadenaMinusculas, char [] cadenaMaysculas){
 
 
        char [] cadenaFinal;
        cadenaFinal= cadena.toCharArray();
 
 
        char [] cadenaChar;
        cadenaChar = cadena.toCharArray();
 
        for (int i = 0; i < cadenaChar.length; i++) {
 
 
            for (int j = 0; j < cadenaMinusculas.length; j++) {
 
                if (cadenaChar[i] == cadenaMinusculas[j]) {
 
                    cadenaFinal[i] = cadenaMaysculas[j];
 
                }
 
                if (cadenaChar[i] == cadenaMaysculas[j]) {
 
                    cadenaFinal[i] = cadenaMinusculas[j];
 
                }
 
            }
 
        }
        System.out.println(cadenaFinal);
    }
 
    public static void main(String[] args) {
        crearFichero();
        String cadena = new String(devolverTesto());
 
        char[] cadenaMinusculas = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'q', 'l', 'm', 'n', 'ñ', 'o', 'p', 'k', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        char[] cadenaMaysculas = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'Q', 'L', 'M', 'N', 'Ñ', 'O', 'P', 'K', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
 
 
        invertirCadena(cadena, cadenaMinusculas, cadenaMaysculas);
    }
 
 
}


Realiza un programa en JAVA que lea un archivo creado en el bloc de notas llamado numeros.txt que contiene varias líneas y en cada una de ellas un número. Luego, el programa te dará la suma de todos los números del fichero.

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
public class Ejercicio09Tema08 {
 
    public static void mostrarFichero() {
        FileReader fr = null;
        try {
            File fichero = new File("numeros.txt");
            fr = new FileReader(fichero);
            BufferedReader br = new BufferedReader(fr);
            leerFichero(br);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
 
    public static void leerFichero(BufferedReader br) throws Exception {
        String linea;
        int suma = 0;
        int num;
 
        linea = br.readLine();
 
        while (linea != null) {
 
            num = Integer.parseInt(linea);
            suma = suma + num;
            linea = br.readLine();
        }
        System.out.println("La suma de los numeros es: " + suma);
    }
 
    public static void main(String[] args) {
        mostrarFichero();
    }
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
2
Comentar

Ejercicios resueltos de ficheros texto

Publicado por domingo (2 intervenciones) el 07/02/2018 13:20:31
hola buenos dias necesito ayuda con un programa me copie el este e hice unas modificaciones pero no estoy muy empapado sera que me pueden ayudar a corregir el codigo?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
-1
Comentar