Monday, 24 March 2014

JAVA IO

Posted By: Unknown - Monday, March 24, 2014


Java.IO package provides wide range of library to deal with Input Output.  Java uses stream to handle input output. There are two types of steam in java
1. Byte Stream : It is also called binary steam. It handles raw data. We can use this steam for images, PDFs, audio, video files ..etc
2. Character Stream : It handles character. It handles translation from local character set.
3. Data Stream : Data Streams allows to write java primitive data type ( byte, short, int long, float, double ..etc) and String ( Non Primitive Data Type). Data Stream can be of two type
i.e. Data Input Stream and Data Output Stream
i) Data Input Stream : It facilitates program to read primitive data type from underline steam.
Example : Adding of two number from console.
       
import java.io.DataInputStream;
public class DataInputStreamEx
{
 public static void main(String args[])
 {
    try
    {
    System.out.println("Example of Addition ");
    DataInputStream dis = new DataInputStream(System.in);
    System.out.println("First Number : ");
    int firstNum =Integer.parseInt(dis.readLine());
    System.out.println("Second Number : ");
    int secondNum = Integer.parseInt(dis.readLine());
    int addition = firstNum + secondNum;
    System.out.println("Sum of "+firstNum+" and  "+secondNum+" = " + addition );

    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
 }
}

Output :
G:\work>java DataInputStreamEx
Example of Addition
First Number :
11
Second Number :
12
Sum of 11 and  12 = 23
ii) Data Output Stream : It facilitates program to convert primitive data type data to binary stream and binary stream can write into file.
Example : Program to write int primitive data type to a file
import java.io.*;
public class DataOutputSteamEx
{
public static void main(String[] args)
{
String fileName = "C://Test.txt";

try
{
//creatiung fileoutstream object and it will be used to write primitive data type
FileOutputStream fos = new FileOutputStream(fileName);

// create dataoutputsteam object
DataOutputStream dos = new DataOutputStream(fos);

int num = 100;
//wirte primitve data to file
dos.writeInt(num);
dos.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
Output : Respective file will be written.
4. Object Stream :
5. Buffered Stream :

0 comments:

Post a Comment

this blog is helpful or not

International

Auto News

Translate

Pages

Popular Posts

Popular Posts

Designed By Templatezy / Sb Game Hacker Apk / MyBloggerThemes