Program
To store and display product details using random access file mode.
Algorithm
Step 1: Start
Step 2: Declare and initialize variable
Step 3: Read fname
Step 4: File rf=new file(fname)
Step 5: Repeat steps 6 to 14 until temp=’y’,increment n by 1
Step 6: Print “Enter product code”
Step 7: Read code
Step 8: Print “Enter product name”
Step 9: Read name
Step 10: Print “Enter product cost”
Step 11: Read cost
Step 12: Print “Enter product stock”
Step 13: Read no
Step 14: Write data to the file
Step 15: Print fname
Step 16: Repeat steps 17-19until n>0
Step 17: Read code,name,cost,no from file
Step 18: Print code,name,cost,no
Step 19: Decrement n by 1
Step 20: Stop
Program
import java.io.*;
class Rand
{
File fn;
RandomAccessFile rf;
int pcode,stock,blk_size;
float cost;
byte bpname[];
String pname;
Rand(String fname)
{
fn=new File(fname);
try
{
rf=new RandomAccessFile(fn,"rw");
}
catch (Exception e)
{
System.out.println("Error : " + e);
System.exit(0);
}
bpname=new byte[20];
blk_size=4*3+20;
}
void AddProduct(DataInputStream in)
{
int i;
try
{
System.out.println("Enter the product code : ");
pcode=Integer.parseInt(in.readLine());
System.out.println("Enter the product name :");
pname=in.readLine();
System.out.println("Enter the product cost : ");
cost=Float.parseFloat(in.readLine());
System.out.println("Enter the product stock : ");
stock=Integer.parseInt(in.readLine());
rf.seek(rf.length());
rf.writeInt(pcode);
for(i=0;iOutput
Menu
-------
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
1
Enter the product code :
101
Enter the product name :
Jam
Enter the product cost :
10
Enter the product stock :
36
Menu
-------
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
1
Enter the product code :
102
Enter the product name :
Cake
Enter the product cost :
25
Enter the product stock :
66
Menu
-------
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
1
Enter the product code :
103
Enter the product name :
Pickle
Enter the product cost :
32
Enter the product stock :
26
Menu
-------
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
1
Enter the product code :
104
Enter the product name :
Soap
Enter the product cost :
12
Enter the product stock :
8
Menu
-------
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
1
Enter the product code :
105
Enter the product name :
Pen
Enter the product cost :
11
Enter the product stock :
10
Menu
-------
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
2
PCODE PNAME COST STOCK
----------------------------------------
101 Jam 10.0 36
102 Cake 25.0 66
103 Pickle 32.0 26
104 Soap 12.0 8
105 Pen 11.0 10
----------------------------------------
Menu
-------
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
3
PCODE PNAME COST STOCK
----------------------------------------
101 Jam 10.0 36
103 Pickle 32.0 26
105 Pen 11.0 10
----------------------------------------
Menu
-------
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
Random Access File in Java is a post from ShoutToWorld - Let's Learn Let's Shout - Helping bloggers and developers.