CAPÍTULO 10 Trabajar con Ficheros
Contenidos 1 2 3 4
Escritura secuencial de bytes texto.txt FS buffer Flujo de Salida hacia Fichero H O L A H O L A matriz de bytes FileOutputStream FS; Byte[ ] buffer = new byte[7]; Fichero FS = new FileOutputStream(“texto.txt”, true); FS = new FileOutputStream(“texto.txt”); Rellenar matriz FS.write(buffer, 0, 3); Ejercicio Página 234
Lectura secuencial de bytes texto.txt FE buffer Flujo de Entrada desde Fichero H O L A H O L A H O L A matriz de bytes FileInputStream FE; Byte[ ] buffer = new byte[7]; Fichero FE = new FileInputStream(“texto.txt”); FE.read(buffer, 0, 3); Ejercicio Página 236
CLASE FILE C: F Hola Mundo …println( F.getName() ) texto.txt …println( F.getParent() ) documentos …println( F.getPath() ) proyecto documentos\texto.txt …println( F.getAbsolutePath() ) documentos C:\proyecto\documentos\texto.txt …println( F.length() ) 10 texto.txt F …println( F.exists() ) Hola Mundo true / false …println( F.isFile() ) true …println( F.isDirectory() ) false File F2 = new File(“…texto2.txt“) F.renameTo(F2) Cambia el nombre del fichero F.delete() File F = new File(“documentos\\texto.txt”); Elimina el fichero
Crea el directorio si no existe File F = new File(“documentos”); CLASE FILE C: F.list() proyecto m[0] reclamación.doc foto.jpg … música.mp3 matriz m m[1] documentos m[...] m[n] reclamación.doc foto.jpg … música.mp3 F F.mkdir() Crea el directorio si no existe F.mkdirs() Crea los directorios que sean necesarios File F = new File(“documentos”);
CLASE FILE C: F Hola Mundo F = new File(“documentos\\texto.txt”); FE proyecto documentos F = new File(“documentos\\texto.txt”); F texto.txt FE = new FileInputStream(“documentos\\texto.txt”); Hola Mundo Flujo de Entrada FE buffer FE = new FileInputStream(F); FE.read(buffer, 0, 10);
Escritura secuencial de datos primitivos texto.txt String nombre Antonio long teléfono 956348745 boolean casado False Data Output Stream (Flujo de Salida de Datos) DOS File Output Stream (Flujo de Salida hacia Fichero) FOS Antonio 956348745 True bytes[] buffer Luis 651897629 False Fichero DataOutputStream DOS; DOS = new DataOutputStream(FOS); FOS = new FileOutputStream(“texto.txt”); FileOutputStream FOS; DOS.writeUTF(nombre); DOS.writeLong(teléfono); DOS.writeBoolean(casado); FOS.write(buffer, 0, 3); Ejemplo en Página 241
Lectura secuencial de datos primitivos texto.txt String nombre long teléfono boolean casado Data InputStream (Flujo de Entrada de Datos) DIS File Input Stream (Flujo de Entrada desde Fichero) FIS Antonio 956348745 True Luis 651897629 False Antonio Hola Mundo Byte[] buffer 956348745 Fichero True DataInputStream DIS; DIS = new DataInputStream(FIS); FIS = new FileInputStream(“texto.txt”); FileInputStream FIS; DIS.readUTF(nombre); FIS.read(buffer, 0, 10); DIS.readLong(teléfono); DIS.readBoolean(casado); Ejemplo en Página 242
Acceso Aleatorio a Ficheros nombre = Agenda.readUTF() ANTONIO teléfono = Agenda.readLong() 956348745 telefonos.bd Agenda.seek(38) RandomAccessFile (Fichero de Acceso Aleatorio) Agenda Antonio 956348745 Pedro 651347645 nombre = Agenda.readUTF() JOSE teléfono = Agenda.readLong() 856181534 Agenda.seek(0) Agenda.writeUTF(“IGNACIO “) Fichero Agenda.writeLong(666555444) RandomAccessFile Agenda; Agenda = new RandomAccessFile(“telefonos.bd”, “rw”); 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 A N T O I 9 5 6 3 4 8 7 P E D R 1 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 3 4 7 6 5 J O S E 8 1 R G I