Tutorial de Java - Ficheros en Java

download Tutorial de Java - Ficheros en Java

of 3

Transcript of Tutorial de Java - Ficheros en Java

  • 7/23/2019 Tutorial de Java - Ficheros en Java

    1/3

    28/10/2015 Tutorial de Java - Ficheros en Java

    http://proton.ucting.udg.mx/tutorial/java/Cap8/fichero.html#creafile 1/3

    1. FicherosCreacin de un objeto FileComprobaciones y Utilidades

    2. Streamsde EntradaObjetos FileInputStream

    1. Apertura de unFileInputStream2. Lectura de un FileInputStream3. Cierre de FileInputStream

    4. Ejemplo: Visualizacin de un ficheroObjetos DataInputStream1. Apertura y cierre de DataInputStream2. Lectura de un DataInputStream

    Streams de entrada de URLs1. Apertura de un Stream de entrada

    3. Streams de SalidaObjetos FileOutputStream

    1. Apertura de un FileOutputStream2. Escritura en un FileOutputStream3. Cierre de FileOutputStream

    4. Ejemplo: Almacenamiento de InformacinStreams de salida con buffer

    1. Creacin de Streams de salida con buffer2. Volcado y Cierre de Streams de salida con buffer

    Streams DataOutput1. Apertura y cierre de objetos DataOutputStream2. Escritura en un objeto DataOutputStream3. Contabilidad de la salida

    4. Ficheros de Acceso AleatorioCreacin de un Fichero de Acceso Aleatorio

    Acceso a la InformacinActualizacin de Informacin

    Todos los lenguajes de programacin tienen alguna forma de interactuar con los sistemas de ficheroslocales Java no es una excepcin.

    Cuando se desarrollan applets para utilizar en red, hay que tener en cuenta que la entrada/salidadirecta a fichero es una violacin de seguridad de acceso. Muchos usuarios configurarn susnavegadores para permitir el acceso al sistema de ficheros, pero otros no.

    Por otro lado, si se est desarrollando una aplicacin Java para uso interno, probablemente ser

    necesario el acceso directo a ficheros.

    Ficheros

    http://proton.ucting.udg.mx/tutorial/java/Cap8/fich_al.html#actualizaaleatoriohttp://proton.ucting.udg.mx/tutorial/java/Cap8/fich_al.html#crearaleatoriohttp://proton.ucting.udg.mx/tutorial/java/Cap8/fich_al.htmlhttp://proton.ucting.udg.mx/tutorial/java/Cap8/fich_al.htmlhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#contabilidadhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#escribedataouthttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#streamouthttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#streamouthttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#cerrarbufferhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#crearbufferhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#buffersalidahttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#buffersalidahttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#cierraoutputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#escribeoutputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#abreoutputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#objetooutputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#urlaperturahttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#urlentradahttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#urlentradahttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#leedatainputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#abredatainputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#verficherohttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#cierrainputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#leeinputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#abreinputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.htmlhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.htmlhttp://-/?-http://-/?-http://-/?-http://-/?-http://proton.ucting.udg.mx/tutorial/java/Cap8/fich_al.html#actualizaaleatoriohttp://proton.ucting.udg.mx/tutorial/java/Cap8/fich_al.html#accesoaleatoriohttp://proton.ucting.udg.mx/tutorial/java/Cap8/fich_al.html#crearaleatoriohttp://proton.ucting.udg.mx/tutorial/java/Cap8/fich_al.htmlhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#contabilidadhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#escribedataouthttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#abredataouthttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#streamouthttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#cerrarbufferhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#crearbufferhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#buffersalidahttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#almacenarhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#cierraoutputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#escribeoutputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#abreoutputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.html#objetooutputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_sal.htmlhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#urlaperturahttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#urlentradahttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#leedatainputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#abredatainputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#datainputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#verficherohttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#cierrainputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#leeinputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#abreinputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.html#objetoinputhttp://proton.ucting.udg.mx/tutorial/java/Cap8/str_ent.htmlhttp://-/?-http://-/?-
  • 7/23/2019 Tutorial de Java - Ficheros en Java

    2/3

    28/10/2015 Tutorial de Java - Ficheros en Java

    http://proton.ucting.udg.mx/tutorial/java/Cap8/fichero.html#creafile 2/3

    Antes de realizar acciones sobre un fichero, necesitamos un poco de informacin sobre ese fichero. Laclase Fileproporciona muchas utilidades relacionadas con ficheros y con la obtencin de informacin

    bsica sobre esos ficheros.

    Creacin de un objeto File

    Para crear un objeto File nuevo, se puede utilizar cualquiera de los tres constructores siguientes:

    File miFichero; miFichero = new File( "/etc/kk" );

    o

    miFichero = new File( "/etc","kk" );

    o

    File miDirectorio = new File( "/etc" ); miFichero = new File( miDirectorio,"kk" );

    El constructor utilizado depende a menudo de otros objetos File necesarios para el acceso. Porejemplo, si slo se utiliza un fichero en la aplicacin, el primer constructor es el mejor. Si en cambio,se utilizan muchos ficheros desde un mismo directorio, el segundo o tercer constructor sern mscmodos. Y si el directorio o el fichero es una variable, el segundo constructor ser el ms til.

    Comprobaciones y Utilidades

    Una vez creado un objeto File, se puede utilizar uno de los siguientes mtodos para reunirinformacin sobre el fichero:

    Nombres de fichero

    String getName() String getPath() String getAbsolutePath() String getParent() boolean renameTo( File nuevoNombre )

    Comprobaciones

    boolean exists()

    boolean canWrite() boolean canRead() boolean isFile() boolean isDirectory() boolean isAbsolute()

    Informacin general del fichero

    long lastModified() long length()

    Utilidades de directorio

    boolean mkdir() String[] list()

    Vamos a desarrollar una pequea aplicacin que muestra informacin sobre los ficheros pasados

    http://proton.ucting.udg.mx/tutorial/java/Fuentes/Cap8/InfoFichero.java
  • 7/23/2019 Tutorial de Java - Ficheros en Java

    3/3

    28/10/2015 Tutorial de Java - Ficheros en Java

    http://proton.ucting.udg.mx/tutorial/java/Cap8/fichero.html#creafile 3/3

    como argumentos en la lnea de comandos, InfoFichero.java:

    import java.io.*;

    class InfoFichero {

    public static void main( String args[] ) throws IOException { if( args.length > 0 ) {

    for( int i=0; i < args.length; i++ ) { File f = new File( args[i] ); System.out.println( "Nombre: "+f.getName() ); System.out.println( "Camino: "+f.getPath() ); if( f.exists() ) { System.out.print( "Fichero existente " ); System.out.print( (f.canRead() ?

    " y se puede Leer" : "" ) ); System.out.print( (f.canWrite() ?

    " y se puese Escribir" : "" ) ); System.out.println( "." ); System.out.println( "La longitud del fichero son "+ f.length()+" bytes" ); } else System.out.println( "El fichero no existe." ); } } else System.out.println( "Debe indicar un fichero." ); } }

    Tutorial de Java [Anterior][Indice][Siguiente]

    http://proton.ucting.udg.mx/tutorial/java/Cap9/comms.htmlhttp://proton.ucting.udg.mx/tutorial/java/Intro/tabla.htmlhttp://proton.ucting.udg.mx/tutorial/java/Cap8/entrada.htmlhttp://proton.ucting.udg.mx/tutorial/java/Fuentes/Cap8/InfoFichero.java