In java, a basic input-output stream provides a method for reading & writing bytes or characters in a file.
If we want to read-write primitive data types (such as int, float, etc.) then we can use filter classes to filter data in the original stream.
DataInputStream & DataOutputStream are two filter classes used for creating “data streams” for handling primitive data types.
1 DataInputStream Class
In java DataInputStream class is used to allow applications to read primitive data from different sources.
DataInputStream is a subtype of FilterInputStream.
A. DataInputStream Constructor
| Sr No | Constructor |
|---|---|
| 1 | DataInputStream(InputStream in) Take InputStream object to create DataInputStream |
Constructor call of DataInputStream
FileInputStream i = new FileInputStream("A.txt"); //open file A for read
DataInputStream I = new DataInputStream(i);
B Methods in DataInputStream
Following are the methods of DataInputStream
| Sr No | Method |
|---|---|
| 1 | public final int read(byte[] b) throws IOException Reads number of bytes from input stream and store in buffered array b |
| 2 | public final int read(byte[] b, int off, int len) throws IOException Reads up to len bytes from input stream and store in buffered array b |
| 3 | public final boolean readBoolean() throws IOException Reads one input byte and return true if byte is non zero, else false |
| 4 | public final byte readByte() throws IOException Reads and return one input byte |
| 5 | public final char readChar() throws IOException Reads two input byte and return a char |
| 6 | public final double readDouble() throws IOException Reads eight input byte and return a double value |
| 7 | public final float readFloat() throws IOException Reads four input bytes and return a float value |
| 8 | public final void readFully(byte[] b) throws IOException Reads some bytes from input stream and store in buffer array b |
| 9 | public final int readInt() throws IOException Reads four input bytes and return a integer value |
| 10 | public final long readLong() throws IOException Reads eight input bytes and returns a long value |
| 11 | public final short readShort() throws IOException Reads one input bytes and returns a short value |
| 12 | public final int readUnsignedByte() throws IOException Reads two byte and return unsigned byte value |
| 13 | public final String readUTF() throws IOException Reads a string that has been encoded in modified utf-8 format |
| 15 | public static final String readUTF(DataInput in) throws IOException Reads from a stream that has been encoded in modified utf-8 format |
| 16 | public final int skipBytes(int n) throws IOException Makes attempt to skip n bytes of data from input stream |
Example: dataOutputStream in java Example
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamExample {
public static void main(String[] s) throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("D:/item.txt"); //Open file “item” for write
DataOutputStream dos = new DataOutputStream(fos);
String itemName = "Laptop";
dos.writeUTF(itemName);
int quantity = 500;
dos.writeInt(quantity);
double price = 70000;
dos.writeDouble(price);
System.out.println("Record saved successfully");
dos.close();
fos.close();
}
}
Example: dataInputStream java example
package com.sspu.javatestproject;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class DataInputStreamExample {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("D:/item.txt"); //open file a for read
DataInputStream dis = new DataInputStream(fis);
System.out.println("Item Details are");
System.out.println("Item Name " + dis.readUTF());
System.out.println("quantity " + dis.readInt());
System.out.println("price " + dis.readDouble());
dis.close();
fis.close();
}
}
name Ram Kumar Item Details are Item Name Laptop quantity 500 price 70000.0
Example: Write a program to write & read primitive data on the same file.
Here we discussed DataInputStream and DataOutputStream to read file data.
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class JavaTestProject {
public static void main(String args[]) throws IOException {
FileOutputStream fos = new FileOutputStream("D:/a.txt"); //Open file “a” for write
DataOutputStream dos = new DataOutputStream(fos);
//write primitive data on file A
String name = "Ram Kumar";
dos.writeUTF(name);
int age = 19;
dos.writeInt(age);
double salary = 130000d;
dos.writeDouble(salary);
boolean preference = false;
dos.writeBoolean(preference);
char gender = 'M';
dos.writeChar(gender);
dos.close();
fos.close();
//read primitive data from file A
FileInputStream fis = new FileInputStream("D:/a.txt"); //open file a for read
DataInputStream dis = new DataInputStream(fis);
System.out.println("name " + dis.readUTF());
System.out.println("age " + dis.readInt());
System.out.println("salary " + dis.readDouble());
System.out.println("preference " + dis.readBoolean());
System.out.println("gender " + dis.readChar());
dis.close();
fis.close();
}
}
Output
name Ram Kumar age 19 salary 130000.0 preference false gender M
Java Scanner Class Getting Input from user and file
Concatenate & Buffer File in Java
Reading a file using FileInputStream
DataInputStream class is used to read primitive data.
DataOutputStream class is used to write primitive data to file.