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