21 How to load data dynamically from file using java.io.DataInputStream in Java?

For Ex: In this I am reading one Boolean , Char, Int and String data from the file.

DISDemo.java

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

class DISDemo{

public static void main(String arg[] )throws IOException{

FileOutputStream fos = new FileOutputStream("ca.text");
DataOutputStream dos=new DataOutputStream(fos);

dos.writeBoolean(true);
dos.writeChar('a');
dos.writeInt(100);
dos.writeChars("Coding Atharva");

FileInputStream fis = new FileInputStream("ca.text");
DataInputStream dis = new DataInputStream(fis);

System.out.println( dis.readBoolean() );
System.out.println( dis.readChar() );
System.out.println( dis.readInt() );
System.out.println( dis.readLine() );
}
}                         

Output: 


Previous
Next Post »