12 nov 2015

Lectura / Escritura de datos de tipos primitivos en Java

Se utilizan las clases DataInputStream y DataOutputStream. Estas clases, además de los métodos read() y write(), proporcionan métodos específicos para cada tipo de dato primitivo.

Ejemplos:
File fich= new File(ruta);
FileInputStream fis= new FileInputStream(fich);
DataInputStream dis= new DataInputStream(fis);

File fich= new File(ruta);
FileOutputStream fos= new FileOutputStream(fich);
DataOutputStream dos= new DataOutputStream(fos);


Lectura/Escritura de datos de tipos primitivos (buffered)

Se pueden utilizar con memoria intermedia. Por ejemplo:

archivo_salida = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(ruta)));

archivo_entrada = new DataInputStream(
new BufferedInputStream(
new FileInputStream(ruta)));

donde ruta denota un ejemplar de File o un ejemplar de String.

Ejemplo de uso:

package flujosdedatos;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FlujosDeDatos {
    static final double[] precios = {19.99, 9.99, 15.99, 3.99, 4.99};
    static final int[] numerosDeUnidades = {12, 8, 13, 29, 50};
    static final String[] descripciones = {"memoria usb", "tarjeta sd",         "teclado", "disco usb", "disco duro"};

    public static void main(String[] args) throws IOException {
        DataOutputStream f_sal = null;

        try {
            f_sal = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("facturacion.bin")));

            for (int i = 0; i < precios.length; i++) {
                f_sal.writeDouble(precios[i]);
                f_sal.writeInt(numerosDeUnidades[i]);
                f_sal.writeUTF(descripciones[i]);
            }
        } finally {
            f_sal.close();
        }
       
        DataInputStream f_ent = null;
        double total = 0.0;
        try {
            f_ent = new DataInputStream(new BufferedInputStream(new FileInputStream("facturacion.bin")));

            double precio;
            int numero_de_unidades;
            String descripcion;

            try {
                while (true) {
                    precio = f_ent.readDouble();
                    numero_de_unidades = f_ent.readInt();
                    descripcion = f_ent.readUTF();
                    total += numero_de_unidades * precio;
                }
            } catch (EOFException e) {
            }
            System.out.format("Importe total: %.2f€%n", total);
        } finally {
            f_ent.close();
        }
    }
}


Flujos de datos – DataOutputStream

void flush()
Vuelca en disco la memoria intermedia
int size()
Proporciona el número de bytes escritos hasta el momento.
void write(byte[] b, int ppio, int num)
Escribe num elementos de byte, empezando en ppio.
void write(int b)
Escribe el byte menos significativo del int que se le proporciona como argumento.
void writeBoolean(boolean v)
Escribe un valor boolean en forma de byte
void writeByte(int v)
Escribe el byte menos significativo del int que se le proporciona como argumento.
void writeBytes(String s)
Escribe la cadena en forma de bytes, descartando el byte superior de cada char
void writeChar(int v)
Escribe un char en forma de 2 bytes, con el MSB en primer lugar.
void writeChars(String s)
Escribe la cadena como una sucesión de caracteres (con 2 bytes cada uno).
void writeDouble(double v)
Traduce v a long empleando el método de Double doubleToLongBits, y escribe ese long en forma de 8 bytes con el MSB en primer lugar.
void writeFloat(float v)
Traduce v a int empleando el método de FloatfloatToIntBits y escribe ese int en forma de 4 bytes con el MSB en primer lugar.
void writeInt(int v)
Escribe el int v en forma de 4 bytes, con el MSB en primer lugar.
void writeLong(long v)
Escribe el long v en forma de 8 bytes, con el MSB en primer lugar.
void writeShort(int v)
Escribe el short v en forma de 2 bytes, con el MSB en primer lugar.
void writeUTF(String str)
Escribe los caracteres de la cadena en formato UTF-8 modificado


Flujos de datos – DataInputStream

int read(byte[] b)
Lee bytes hasta llenar b o acabar el fichero. Devuelve el número de bytes leídos, o -1 si el archivo está vacío.
int read(byte[] b, int off, int ppio)
Lee hasta len bytes, almacenándolos a partir de ppio.
boolean readBoolean()
Lee un byte y proporciona false si es 0 o true si no.
byte readByte()
Lee un byte.
char readChar()
Lee 2 bytes y los devuelve en forma de carácter.
double readDouble()
Lee 8 bytes y los devuelve en forma de double.
float readFloat()
Lee 4 bytes y los devuelve en forma de float.
void readFully(byte[] b)
Intenta llenar b de bytes. Si acaba el archivo o hay un error de E/S lanza una EOFException o IOException.
void readFully(byte[] b, int ppio, int num)
Intenta leer num bytes, almacenándolos a partir de ppio.
int readInt()
Inverso de writeInt(), lee 4 bytes y devuelve un int. Supone que el MSB está a la izquierda.
long readLong()
Inverso de writeLong(), lee 8 bytes y devuelve un long. Supone que el MSB está a la izquierda.
short readShort()
Inverso de writeShort(), lee 2 bytes y devuelve un short. Supone que el MSB está a la izquierda.
int readUnsignedByte()
Lee un byte y lo extiende por ceros para dar un int, entre 0 y 255. Inverso de writeByte hasta 255.
int readUnsignedShort()
Lee dos bytes para dar un int, entre 0 y 65535. Inverso de writeShort hasta 65535.
String readUTF()
Lee cadenas escritas con writeUTF.
static String readUTF(DataInput in)
Lee cadenas escritas con writeUTF en el ejemplar de DataInput aportado
int skipBytes(int n)
Intenta saltar n bytes, proporciona el número de bytes que ha saltado. No lanza excepciones.


No hay comentarios:

Publicar un comentario