DataInputStream:
DataInputStream used to read primitive data types from an input source.
Commonly used constructors of DataInputStream:
DataInputStream(InputStream in)
Creates a DataInputStream that uses the specified underlying InputStream.
Commonly used methods of DataInputStream:
public String readLine() throws IOException
Reads the next line of text from the input stream. It reads successive bytes, converting each byte separately into a character, until it encounters a line terminator or end of file; the characters read are then returned as a String.
i. public final Boolean readBooolean()throws IOException
ii. public final byte readByte()throws IOException
iii. public final short readShort()throws IOException
iv. public final Int readInt()throws IOException
These methods will read the bytes from the contained InputStream. Returns the next two bytes of the InputStream as the specific primitive type.
Example:
DataInputStreamExample.java
import java.io.DataInputStream; import java.io.FileInputStream; /** * This program is used to read a file using DataInputStream. * @author w3schools */ class IOTest{ public void readFile(){ try { //creating FileInputStream object. FileInputStream fis = new FileInputStream("F:\\New folder\\data1.txt"); //Creating DataInputStream object. DataInputStream dis = new DataInputStream(fis); int i; //read file. while((i=dis.read())!=-1){ System.out.print((char)i); } } catch (Exception e) { e.printStackTrace(); } } } public class DataInputStreamExample { public static void main(String args[]){ //creating IOTest object. IOTest obj = new IOTest(); //method call. obj.readFile(); } } |
Output:
Hello.
This is a text file. |
DataOutputStream:
DataOutputStream used to write primitive data types to an output source.
Commonly used constructors of DataOutputStream:
DataOutputStream(OutputStream out)
Creates a new data output stream to write data to the specified underlying output stream.
Commonly used methods of DataOutputStream:
public final void writeBytes(String s) throws IOException
Writes out the string to the underlying output stream as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits.
i. public final void writeBooolean()throws IOException
ii. public final void writeByte()throws IOException
iii. public final void writeShort()throws IOException
iv. public final void writeInt()throws IOException
These methods will write the specific primitive type data into the output stream as bytes.
Example:
import java.io.DataOutputStream; import java.io.FileOutputStream; /** * This program is used to write data * into a file using DataOutputStream. * @author w3schools */ class IOTest{ String str = "Hello www.w3schools.com"; public void writeFile(){ try { //Creating FileOutputStream object. //It will create a new file before writing if not exist. FileOutputStream fos = new FileOutputStream("F:\\New folder\\data4.txt"); //Creating DataOutputStream object. DataOutputStream dos = new DataOutputStream(fos); byte b[]=str.getBytes(); dos.write(b); dos.flush(); //Close file after write operation. dos.close(); System.out.println("Contents written successfully."); } catch (Exception e) { e.printStackTrace(); } } } public class DataOutputStreamExample { public static void main(String args[]){ //Creating IOTest object. IOTest obj = new IOTest(); //method call obj.writeFile(); } } |
Output:
Contents written successfully. |
Download this example.
Next Topic: BufferedInputStream and BufferedOutputStream in java with example.
Previous Topic: Byte Streams in java with example.