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.
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.
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”.
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


0