Tuesday 27 September 2011

JAVAI/O


              JAVAI/O
Aim:
            To write a java program to implement the concept of I/O.
Algorithm:
Step1: Start.
Step2: Import the classes util and io.
Step3: Define a class inventory which define the void main().
Step4: Get the item code, no of  items and item cost from the user.
Step5: Using the DataOutputStream class write the item details inside the file invent.dat.
Step6: Using the DataInputStream class retrieve the stored details from the file and calculate the total cost.
Step7: Display the item code, no of  items, cost and total cost in the console.
Step8: Stop.
Program:
import java.util.*;
import java.io.*;
class inventory
{
static DataInputStream din=new DataInputStream(System.in);
static StringTokenizer st;
public static void main(String args[]) throws IOException
{
DataOutputStream dos= new DataOutputStream(new FileOutputStream("invent.dat"));
System.out.println("enter code number:");
st=new StringTokenizer(din.readLine());
int code= Integer.parseInt(st.nextToken());

System.out.println("enter number of items:");
st=new StringTokenizer(din.readLine());
int items= Integer.parseInt(st.nextToken());

System.out.println("enter cost:");
st=new StringTokenizer(din.readLine());
double cost= new Double(st.nextToken()).doubleValue();
dos.writeInt(code);
dos.writeInt(items);
dos.writeDouble(cost);
dos.close();

DataInputStream dis = new DataInputStream(new FileInputStream("invent.dat"));
int codeNumber = dis.readInt();
int totalItems = dis.readInt();
double itemCost = dis.readDouble();
double totalCost = totalItems * itemCost;
dis.close();
System.out.println();
System.out.println("Code Number:"+ codeNumber);
System.out.println("Item Cost :"+ itemCost);
System.out.println("totalitems :"+ totalItems);
System.out.println("totalcost :"+totalCost);
}
}

Output:
Z:\anitha>javac -d inventory.java
Z:\anitha>java inventory
enter code number:
101
enter number of items:
10
enter cost:
400
Code Number:101
Item Cost :400.0
totalitems :10
totalcost :4000.0

0 comments:

Post a Comment