Java Tutorial

Data-Stream

Data Input Stream class:

This is used for two purposes. They are reading the data from input device like keyboard and reading the data from remote machine like server.

In order to perform the above operations we must create an object of Data Input Stream class.

Constructors:

DataInputStream (InputStream);

For example:

DataInputStream dis=new DataInputStream (System.in);

An object of InputStream called 'in' is created as a static data member in System class.

Instance methods:

1.	public byte readByte ();
2.	public char readChar ();
3.	public short readShort ();
4.	public int readInt ();
5.	public long readLong ();
6.	public float readFloat ();
7.	public double readDouble ();
8.	public boolean readBoolean ();
9.	public String readLine ();

Data Output Stream class:

This is used for displaying the data onto the console and also used for writing the data to remote machine.

Constructor:

DataOutputStream (OutputStream);

For example:

DataOutputStream dos=new DataOutputStream (System.out);

Instance methods:

Old methods:

1.	public void writeByte (byte);
2.	public void writeChar (char);
3.	public void writeShort (short);
4.	public void writeInt (int);
5.	public void writeLong (long);
6.	public void writeFloat (float);
7.	public void writeDouble (double);
8.	public void writeBoolean (boolean);
9.	public void writeLine (String);

New methods:

1.	public void write (byte);
2.	public void write (char);
3.	public void write (short);
4.	public void write (int);
5.	public void write (long);
6.	public void write (float);
7.	public void write (double);
8.	public void write (boolean);
9.      public void write (String);

Write a JAVA program to read two numbers from keyboard and display their product (using older methods)?

Answer:

import java.io.*; 
class DataRead
{
    public static void main (String [] args)
    {
        try
        {
            DataInputStream dis=new DataInputStream (System.in); 
            System.out.println ("Enter first number : ");  
            String s1=dis.readLine ();
            System.out.println ("Enter second number : "); 
            String s2=dis.readLine ();
            int n1=Integer.parseInt (s1);
            int n2=Integer.parseInt (s2); 
            int n3=n1*n2;
            System.out.println ("Product = "+n3);
        }
        catch (FileNotFoundException fnfe)
        {
            System.out.println ("FILE DOES NOT EXISTS");
        }
        catch (IOException ioe)
        {
            System.out.println (ioe);
        }
        catch (NumberFormatException nfe)
        {
            System.out.println ("PASS INTEGER VALUES ONLY");
        }
    }
};

Implement copy command of DOS?

Answer:

import java.io.*;
class	Xcopy
{
    public static void main(String[] args)
    {
        FileInputStream fis=null;
        FileOutputStream fos=null; if (args.length!=2)
        {
            System.out.println ("INVALID ARGUMENTS..!");
        }
        else
        {
            try
            {
                fis=new FileInputStream (args [0]);
                fos=new FileOutputStream (args [1], true); 
                int i;
                do
                {
                    i=fis.read ();
                    char ch=(char)i;// type casting int value into char value
                    fos.write (ch);
                }
                while (i!=-1);
            }
            catch (FileNotFoundException fnfe)
            {
                System.out.println (args [0]+"DOES NOT EXIST..!");
            }
            catch (IOException ioe)
            {
                System.out.println (ioe);
            }
            catch (Exception e)
            {
                e.printStackTrace ();
            }
            finally
            {
                try
                {
                    if (fis!=null)// file is closed when it is opened
                    {
                        fis.close ();
                    }
                    if (fos!=null)
                    {
                        fos.close ();
                    }
                }
                catch (IOException ioe)
                {
                    ioe.printStackTrace ();
                }
                catch (Exception e)
                {
                     e.printStackTrace ();
                }
            }// finally
        }// else
    }//main
};

Implement type command of DOS?

Answer:

import java.io.*;
class DosType
{
    public static void main (String [] args)
    {
        FileInputStream fis=null;
        if (args.length!=2)
        {
            System.out.println ("INVALID ARGS");
        }
        else
        {
            try
            {
                fis=new FileInputStream (args [0]);
                int i; do
                {
                    i=fis.read ();
                    char ch=(char) i; 
                    System.out.println (ch);
                }
                while (i!=-1);
            }
            catch (FileNotFoundException fnfe)
            {
                System.out.println (args [0]+" DOES NOT EXIT");
            }
            catch (Exception e)
            {
                e.printStackTrace ();
            }
            finally
            {
                try
                {
                    if (fis!=null)
                    {
                        fis.close ();
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace ();
                }
            }// finally
        }// else
    }// main
};// DosType