Java-Reading Writing Primitive Data Types

Reading Writing Primitive Data Types

The basic reading and writing streams provide read/write methods that can only be used for reading/writing bytes or characters. If we want to read/write the primitive data types such as integers and doubles, we can use filter classes as wrappers on existing reading and writing streams to filter data in the original stream. The two filter classes used for creating “data streams” for handling primitive types are DatalnputStream and DataOutputStream.

The following program demonstrates the use of data streams for reading and writing primitive data types. The program first creates “data.da” file and then writes a few primitive data types into it using DataOutputStream. At the end of writing, the streams are closed. The program also creates a DataInputStream, and connects it to “data.da” file. It then reads data from the file and displays them on the screen. Finally, it closes the streams.

DataOutputStream Constructors

Constructor Description
DataOutputStream(OutputStream out) Creates a new data output stream to write data to the specified underlying output stream.

DataOutputStream Methods

Method Description
void write(int b) This method used to write the specified byte(int to byte) to the underlying output stream.
void write(byte[] b) This method used to write b.length bytes from the specified byte array to the underlying output stream.
void write(byte[] b, int off, int len) This method used to write len bytes from the specified byte array starting at offset off to the underlying output stream.
void writeBoolean(boolean v) This method used to write the specified boolean to the underlying output stream as a 1-byte value.
void writeByte(int v) This method used to write the specified byte(int to byte) to the underlying output stream.
void writeBytes(String s) This method used to write the specified string to the underlying output stream as a sequence of bytes.
void writeChar(int v) This method used to write the specified char to the underlying output stream as a 2-byte value, high byte first.
void writeChars(String s) This method used to write the specified string to the underlying output stream as a sequence of characters.
void writeInt(int v) This method used to write the specified int to the underlying output stream as four bytes, high byte first.
void writeFloat(float v) Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.
void writeDouble(double v) Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first.
void writeLong(long v) This method used to write the specified long to the underlying output stream as eight bytes, high byte first.
void writeShort(int v) This method used to write the specified short to the underlying output stream as two bytes, high byte first.
void writeUTF(String str) This method used to write the specified string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.
int size() This method used to return the current value of the counter written, the number of bytes written to this data output stream so far.
void close() This method used to close the stream.

DataInputStream Constructors

Constructor Description
DataInputStream(InputStream in) Creates a DataInputStream that uses the specified underlying InputStream.

DataInputStream Methods

Method Description
int read() Reads a byte of data from the underlying input stream.
int read(byte[] b) Reads up to b.length bytes of data from the underlying input stream into an array of bytes.
int read(byte[] b, int off, int len) Reads up to len bytes of data from the underlying input stream into an array of bytes.
boolean  readBoolean() Reads a boolean data from the underlying input stream as a 1-byte value.
byte        readByte() Reads a byte of data from the underlying input stream.
char        readChar() Reads a character data from the underlying input stream.
double    readDouble() Reads a double data from the underlying input stream.
float        readFloat() Reads a float data from the underlying input stream.
int           readInt() Reads a integer data from the underlying input stream.
long        readLong() Reads a long data from the underlying input stream.
short       readShort() Reads a short data from the underlying input stream.
int           readUnsignedByte() Reads a unsigned byte from the underlying input stream.
int           readUnsignedShort() Reads a unsigned short from the underlying input stream.
String     readUTF() Reads a String in modified UTF-8 format from the underlying input stream.
void        readFully(byte[] b) Reads up to b.length bytes of data from the underlying input stream into an array of bytes.
void        readFully(byte[] b, int off, int len) Reads up to len bytes of data from the underlying input stream into an array of bytes.
int           available() Returns the number of remaining bytes that can be read from the underlying input stream.
long        skip(long n) Skips over and discards n bytes of data from the input stream.
void        close() Closes the stream and releases any system resources associated with it.

Program

final9

Program Source

import java.io.FileOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.IOException;

class Javaapp {
    
    public static void main(String[] args) throws IOException {
        
        FileOutputStream fos = new FileOutputStream("data.da");
        DataOutputStream dos = new DataOutputStream(fos);
        dos.writeInt(50000);
        dos.writeShort(2500);
        dos.writeByte(120);
        dos.close();
        
        FileInputStream fis = new FileInputStream("data.da");
        DataInputStream dis = new DataInputStream(fis);
        System.out.println("Int   : "+dis.readInt());
        System.out.println("Short : "+dis.readShort());
        System.out.println("Byte  : "+dis.readByte());
        dis.close();        
    }
}

Leave a Comment