Thursday, 22 September 2016

Java Tutorial : Java IO (DataOutputStream DataInputStream Example)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=OeL1GLYw8-M&list=UUhwKlOVR041tngjerWxVccw

DataOutputStreamDemo.java
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataOutputStreamDemo
{

    public static void main(String[] args) throws Exception
    {
        DataOutputStreamDemo dataOutputStreamDemo = new DataOutputStreamDemo();
        dataOutputStreamDemo.writePrimitiveData();
    }

    public void writePrimitiveData() throws IOException
    {
        DataOutputStream dataOutputStream = null;
        FileOutputStream fileOutputStream = null;
        try
        {
            fileOutputStream = new FileOutputStream("data.txt");
            dataOutputStream = new DataOutputStream(fileOutputStream);
            int[] intArray = { 100, 250, 440, 550, 900 };

            for (int j : intArray)
            {
                dataOutputStream.writeInt(j);
            }

            System.out.println("Successfully written to \'data.txt\' file\n");
        }
        finally
        {
            if (dataOutputStream != null)
            {
                /*
                 * Closing a DataOutputStream will also
                 * close the OutputStream instance to which
                 * the DataOutputStream is writing.
                 */
                dataOutputStream.close();
            }
        }

    }
}
Output
Successfully written to 'data.txt' file
data.txt
DataInputStreamDemo.java
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class DataInputStreamDemo
{

    public static void main(String[] args) throws Exception
    {
        DataInputStreamDemo dataInputStreamDemo = new DataInputStreamDemo();
        dataInputStreamDemo.readPrimitiveData();
    }

    public void readPrimitiveData() throws IOException
    {
        FileInputStream fileInputStream = null;
        DataInputStream dataInputStream = null;

        try
        {
            fileInputStream = new FileInputStream("data.txt");
            dataInputStream = new DataInputStream(fileInputStream);

            while (dataInputStream.available() > 0)
            {
                int intValue = dataInputStream.readInt();
                System.out.println(intValue);
            }

        }
        finally
        {
            if (dataInputStream != null)
            {
                /*
                 * Closing a DataInputStream will also close
                 * the InputStream instance from which the
                 * DataInputStream is reading.
                 */
                dataInputStream.close();
            }
        }

    }

}
Output
100
250
440
550
900

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_DataInput_and_Output_Stream_Example_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_DataInput_and_Output_Stream_Example_App

Bitbucket Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_DataInput_and_Output_Stream_Example_App

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment