Professional Documents
Culture Documents
Ejemplo de como mostra un contenido en consola: public class DemoMuestraDataEnPantalla { public static void main(String args[]) { int b; b = 'X'; System.out.write(b); // con esto se muestra en la pantalla el valor de la variable b System.out.write('\n'); } }
Ejemplo 2 Crear una clase que lea un archivo y muestre su contenido en pantalla. Usaremos: la Clase FileInputStream
Un archive se abre creando un objeto de la Clase FileInputStream. Usualmente se usa el siguiente constructor: FileInputStream(String nombreArhivo) throws FileNotFoundException
En netbeans
Salida:
Como ya sabemos leer el contenido de archivo ahore veremos como se escribe dentro de otro archivo.
ver ejemplo
/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package estructuradatos_archivos;
public static void main(String args[]) throws IOException { int i; FileInputStream fin; FileOutputStream fout; try { // abriendo el archivo de entrada desde donde vamos a copiar try { fin = new FileInputStream("D:\\ListaOficinas.txt"); } catch(FileNotFoundException exc) { System.out.println("Archivo de entrada no encontrado"); return; } // abriendo el archivo destinos hacia donde vamos a copiar el contenido try { fout = new FileOutputStream("D:\\CopiaListaOficinas.txt"); } catch(FileNotFoundException exc) { System.out.println("Error abriendo el archivo destino, tal vez no se encuentra el arhivo "); return; } } catch(ArrayIndexOutOfBoundsException exc) { System.out.println("Usage: CopyFile From To"); return; }
// copiamos el archiov try { do { i = fin.read(); //leemos if(i != -1){ fout.write(i); //copiamos } } while(i != -1);//hasta llegar al final de primer archivo } catch(IOException exc) { System.out.println("File ocurrio un error"); } fin.close(); fout.close(); System.out.println("Se completo con exito"); } }