Вы находитесь на странице: 1из 3

UNIVERSIDAD DE COSTA RICA

Cree programas donde pueda ejecutar los siguientes segmentos de


cdigo y comente tanto el cdigo, como lo que realizan:
1. Escritura:
String []vectorCadenas = {"hola", "que", "tal", "vamos", "genial"};
PrintWriter salida;
salida = new PrintWriter( new BufferedWriter( new FileWriter("Datos.out")));
int lineCount = 1;
for (int i=0; i<vectorCadenas.length; i++) {
salida.println(lineCount++ + ": " + vectorCadenas[i]);
}
salida.close();

2. Lectura:
BufferedReader in = new BufferedReader( new
FileReader("Datos.out"));
String cadena, cadena2 = new String();
while((cadena = in.readLine())!= null)
cadena2 += cadena + "\n";
in.close();
System.out.print(cadena2);

3. Ms interesante:
DataOutputStream salida = new
DataOutputStream(new
BufferedOutputStream(new
FileOutputStream("Datos.txt")));
salida.writeDouble(3.14159);
salida.writeBytes("Ese es PI\n");
salida.writeChars( "supercalifragilisticoespialidos\n");
salida.writeDouble(1.41413);
salida.writeUTF("Raz cuadrada de dos\n");
salida.close();

______________________________
Profesor: Magster Luis Gustavo Esquivel Quirs

luis.esquivel@ucr.ac.cr

UNIVERSIDAD DE COSTA RICA

4. Un poco ms complicado:
File ruta = new File("<<AQUI PONGA LA RUTA DE SU ESCRITORIO>> ");
File f = new File(ruta, "Datos.txt");
System.out.println(f.getAbsolutePath());
System.out.println(f.getParent());
System.out.println(ruta.getAbsolutePath());
System.out.println(ruta.getParent());
if (!f.exists()) {
System.out.println("Fichero " + f.getName() + " no existe");
if (!ruta.exists()) {
System.out.println("El directorio " + ruta.getName() + " no existe");
if (ruta.mkdir()) {
System.out.println("Directorio creado");
if (f.createNewFile()) {
System.out.println("Fichero " + f.getName() + " creado");
} else {
System.out.println("No se ha podido crear " + f.getName());
}
} else {
System.out.println("No se ha podido crear " + ruta.getName());
}
} else {
if (f.createNewFile()) {
System.out.println("Fichero " + f.getName() + " creado");
} else {
System.out.println("No se ha podido crear " + f.getName());
}
}
} else {
System.out.println("Fichero " + f.getName() + " existe");
System.out.println("Tamao " + f.length() + " bytes");
}

5. Acceso aleatorio: Documente y modifique el cdigo segn lo considere necesario.


static Scanner sc = new Scanner(System.in);
static RandomAccessFile fichero = null;
public static void main(String[] args) {
int numero;
try {
fichero = new RandomAccessFile("Datos.dat", "rw");
mostrarFichero();

______________________________
Profesor: Magster Luis Gustavo Esquivel Quirs

luis.esquivel@ucr.ac.cr

UNIVERSIDAD DE COSTA RICA

System.out.print("Introduce un nmero entero para aadir al final del fichero: ");


numero = sc.nextInt();
fichero.seek(fichero.length());
fichero.writeInt(numero);
mostrarFichero();
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
try {
if (fichero != null) {
fichero.close();
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public static void mostrarFichero() {
int n;
try {
fichero.seek(0);
while (true) {
n = fichero.readInt();
System.out.println(n);
}
} catch (EOFException e) {
System.out.println("Fin de fichero");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}

6. Un ejemplo divertido: Documente todas las lneas de las clases


ProcesamientoImagen y Principal, modifique los nombres de variables y
mtodos para que sean significativos segn corresponda y comente usando
JavaDoc.
7. Agregue al ejemplo anterior la funcionalidad de guardar la imagen.
8. Utilizando los ejemplos trabajados hasta el momento, cree un programa que
permita leer un archivo de texto y presente en pantalla una tabla con las
frecuencias de cada uno de los elementos que lo componen.

______________________________
Profesor: Magster Luis Gustavo Esquivel Quirs

luis.esquivel@ucr.ac.cr

Вам также может понравиться