• August 30, 2014
  • Core Java
  • Comments Off on DataInputStream and DataOutputStream in java

DataInputStream and DataOutputStream in java

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 javawithease
 */
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.

Download this example.

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 javawithease
 */
class IOTest{
	String str = "Hello www.javawithease.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.

Related Topics:

Input output (I/O) in java.
FileInputStream and FileOutputStream in java with example.
Byte Streams in java with example.
DataInputStream and DataOutputStream in java with example.
BufferedInputStream and BufferedOutputStream in java with example.
FileReader and FileWriter in java with example.
How To Check If A File Exists In Java.
Serialization in java with example.
Transient in java with example.

 


Comments are closed.