Tuesday, September 30, 2008

DSA : FILE MANAGEMENT

import java.io.*;

class Record {
RandomAccessFile file=null;//creating object of class RandomAccessFile
String name;
String tel;
Record(){} //default Constructor

public void add(String x,String y) { //Function for adding new entry
name=x;
tel=y;
try {
file=new RandomAccessFile(name,"rw");
file.writeChars(y);
file.close();
}
catch(Exception e) {}
}
public void search(String x) //Function for searching entry
{
try {
file=new RandomAccessFile(x,"rw");
file.seek(0);
int n=tel.length();
String temp=" ";
while(n>0){
temp=temp+file.readChar();
n--;
}
System.out.println("TEL NO:"+temp);
file.close();
}
catch(Exception e) {System.out.println("File not Found:");
}
}
public void delete(String x) { //Function for deleting entry
try {
file=new RandomAccessFile(name,"rw");
file.seek(0);
file.writeChars(" ");
file.close();
}
catch(Exception e) {
System.out.println("File not Found:");
}
}
}

class oper {
public static void main(String[] arg)throws IOException {
Record c1=new Record();
int j=0;
String a;
String b;
try {
DataInputStream x=new DataInputStream(System.in);
while(j<4)
{
System.out.println("Please enter ur choice:"); //Switch loop for choice
System.out.println("1.ADD A NEW RECORD ");
System.out.println("2.SEARCH & DISPLAY EXISTING RECORD");
System.out.println("3.DELETE A CURRENT RECORD");

System.out.println("4.EXIT");

j=Integer.parseInt(x.readLine());
switch(j)
{
case 1:
System.out.println("ENTER THE NAME: ");
a=x.readLine();
System.out.println("ENTER THE NO: ");
b=x.readLine();
c1.add(a,b);
break;
case 2:
System.out.println("ENTER THE NAME: ");
a=x.readLine();
c1.search(a);
break;

case 3:
System.out.println("ENTER THE NAME: ");
a=x.readLine();
c1.delete(a);
break;
case 4:
System.out.println("THANK U: ");
break;
}
}
}
catch(Exception e) {}
}
}

No comments:

Post a Comment

JTSEARCH