CodesJava

Easy learning with example program codes

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

 

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap