Está en la página 1de 14

FLUJOS DE ENTRADA Y

SALIDA Y MANEJO DE
ARCHIVOS EN JAVA
Autor: Ing. Christian Antn

Todo programa necesita comunicarse con su entorno, ya sea para


enviar o recibir informacin. La manera de representar las entradas
y las salidas en Java es a base de STREAMS (Flujos de datos)
FUENTE
(Obtener/leer/recibir/entra
da)
DESTINO
(Entregar/escribir/enviar/sa
lida)

Entre
otros

APLICACIN en
Java

STREAMS
(Conexin)
DATOS
EN
SERIE

ENTRADA Y SALIDA ESTANDAR (TECLADO


Y PANTALLA)

System.in
Paquete: java.langSystem.out
System.err
Clase: System
Mtodos
Objetos: in, out,
err principales

System.setIn(InputStream
System.setOut(PrintStream
System.setErr(PrintStream

Mtodos para
alterar
los dispositivos
estndar de
entrada y salida

LEER Y ESCRIBIR POR CONSOLA

import java.io.*;
public class flujos{
public static void main(String[] x){
String y="";
System.out.println("Hola como te llamas?");
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try{
y = br.readLine();
}catch(IOException e){}
System.out.println("Usted se llama "+y);
}
}

Java.
io

BYTES
Clases:
InputStream
OutputStream
Mtodos:
read( )
write( )

CARACTE
RES
Clases:
Reader
Writer

Jerarqua de las clases flujo

Jerarqua de las clases flujo

Clases que indican origen o


destino

Clases que aaden caractersticas

Tipo de Informacin
Contexto:
Contenido:
Bytes:

File
FileInputStream (Leer)
FileOutputStream
(Escribir)

Caracteres: FileReader (Leer)


FileWriter (Escribir)

Construcci
on :
File O
String ruta

eOutputStream fos = new FileOutputStream(Archivo.txt);

e f1 = new File(Archivo.txt);
FileReader fr1 = new FileReader(Archivo.txt)
eOutputStream fos = new FileOutputStream(f1);
File f1 = new File(Archivo.txt);
FileReader fr1 = new FileReader(f1);

Clase File

Archivo o Carpeta

CONSTRUCTORES
File (String name)
File (String dir, String name)
File (File dir, String name)

Clase File

Archivo o Carpeta

Leyendo Archivos de
Texto
String texto = new String();
try{
BufferedReader br = new BufferedReader(new FileReader(archivo.txt));
String s;
while((s=br.readLine()) != null)
texto +=s;
br.close();
}catch(java.io.FileNotFoundException fnfe){
System.out.println(Archivo no ecnotrado + fnfe);
}
catch(java.io.IOException ioe){ }

Escribiendo Archivos
de Texto

PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(archivo.txt


pw.println(Hola a todos);
pw.print(135.20);
pw.println(Otra linea);
pw.close();
h(java.io.IOException ioe){ }

También podría gustarte