Está en la página 1de 13

Archivos binarios

ObjectInputStream
ObjectOutputStream

Clase ObjectOutputStream
Es usada para escribir en un archivo binario.
Sintaxis:
public ObjectOutputStream (OutputStream objeto)
Ejemplo de su uso:
new ObjectOutputStream(new FileOutputStream (nombreArchivo))
Ojo:
Puede generar FileNotFoundException de IOException

Mtodos
public void writeInt(int n) throws IOException
public void writeShort(short n) throws IOException
public void writeLong(long n) throws IOException
public void writeDouble(double x) throws IOException
public void writeFloat(float x) throws IOException

Mtodos
public void writeBoolean(boolean b) throws IOException
public void writeChar(int n) throws IOException
public void writeObject(Objetc objeto) throws IOException
public void close( ) throws IOException
public void flush( ) throws IOException

Clase ObjectInputStream
Sintaxis:
public ObjectInputStream (InputStream objeto)
Ejemplo de su uso:
new public ObjectInputStream (new FileInputStream(nombreArchivo))

Mtodos
public int readInt( ) throws IOException
public short readShort( ) throws IOException
public long readLong( ) throws IOException
public double readDouble( ) throws IOException
public float readFloat( ) throws IOException
public char readChar( ) throws IOException
public boolean readBoolean( ) throws IOException

Mtodos
public String readUTF( ) throws IOException
public void readObject(Objetc objeto) throws ClassNotFoundException,
IOException
public int skipBytes(int n) throws IOException
public void close( ) throws IOException
public void flush( ) throws IOException

Nota: todos los mtodos de ObjectInputStream lanzan EOFException cuando se ha


alcanzado el final del archivo.

Acceso Aleatorio
Si necesitamos un archivo de acceso aleatorio podemos usar la clase
RandomAccesFile, la cual nos va a permitir leer y escribir en el archivo.
Esta clase la encontramos en el paquete java.io.

Sintaxis
public RandomAccessFile(String nombreArchivo, String modo)
public RandomAccessFile(File ObjetoFile, String modo)
Abre el archivo, no lo borra si ya exista y coloca el puntero al inicio del archivo.
Valor de
modo

Significado

Abre el archivo para lectura solamente

rw

Abre el archivo para leer y escribir

Mtodos
public long getFilePointer() throws IOException
public void seek(long location) throws IOException
public long length() throws IOException
public void setLength(long newLength) throws IOException
public void close() throws IOException
public void write(int b) throws IOException
public void write(byte[] a) throws IOException

public final void writeByte(byte b) throws IOException


public final void writeShort(short n) throws IOException
public final void writeInt(int n) throws IOException
public final void writeLong(long n) throws IOException
public final void writeDouble(double d) throws IOException
public final void writeFloat(float f) throws IOException
public final void writeChar(char c) throws IOException
public final void writeBoolean(boolean b) throws IOException

public final void writeUTF(String s) throws IOException


public int read() throws IOException
public int read(byte[] a) throws IOException
public final byte readByte() throws IOException
public final short readShort() throws IOException
public final int readInt() throws IOException
public final long readLong() throws IOException

public final double readDouble() throws IOException


public final float readFloat() throws IOException
public final char readChar() throws IOException
public final boolean readBoolean() throws IOException
public final String readUTF() throws IOException

También podría gustarte