You are on page 1of 3

import java.util.

*;
import java.io.*;
public class Lab2
{
final int size = 50;
Scanner scan = new Scanner(System.in);
public static void main(String[] args) throws IOException,
NullPointerException{
Lab2 obj = new Lab2();
int ch;
while(true)
{
System.out.println("**********************");
System.out.println("1.Pack()");
System.out.println("2.Unpack()");
System.out.println("3.Search()");
System.out.println("4.Modify()");
System.out.println("5.Exit");
System.out.println("**********************");

System.out.println("Enter your choice:");


ch = obj.scan.nextInt();
obj.scan.nextLine();
switch(ch)
{
case 1:
obj.pack();
break;
case 2:
obj.unpack();
break;
case 3:
obj.search();
break;
case 4:
obj.modify();
break;
case 5:
System.out.println("You chose exit");
System.exit(0);

default:
System.out.println("Invalid options");
}
}
}

public void pack() throws FileNotFoundException


{

System.out.println("Enter name,usn,sem, and branch");

String name = scan.nextLine();


String usn = scan.nextLine();
String sem = scan.nextLine();
String branch = scan.nextLine();

PrintWriter pw = new PrintWriter(new FileOutputStream(new File


("student.txt"),true));
String b = name + "|" + usn+ "|" +sem+ "|" +branch+ "|";
int len =b.length();
String s1= "*";//�-�
if(len<50)
{
for(int j=len;j<=50;j++)
b.count(s1);
}
pw.println(b);
pw.flush();
pw.close();
}

public void unpack() throws IOException


{
String name = " ", usn=" ", sem = " ", branch =" ",s;
BufferedReader br = new BufferedReader(new FileReader("student.txt"));
while((s=br.readLine())!=null)
{
String[] result = s.split( "\\ |");
name=result[0];
usn=result[1];
sem=result[2];
branch=result[3];
System.out.println("The details are :" +name+" "+usn+ " " +sem+ " "+branch);
}
br.close();
}

public void search() throws FileNotFoundException,IOException


{

BufferedReader br = new BufferedReader(new FileReader("student.txt"));


String name =" " , usn =" ", sem = " ", branch =" ",r;
System.out.println("Enter the usn to be searched");
String usn1=scan.nextLine();
while((r=br.readLine())!=null)
{

String[] result = r.split( "\\ |");


name=result[0];
usn=result[1];
sem=result[2];
branch=result[3];
if(usn.equals(usn1))
{
System.out.println("Match found, the details of search are :" );
System.out.println(name+" "+usn+ " " +sem+ " " +branch);

br.close();
return;
}
}
System.out.println("Record not found");
br.close();
}
public void modify() throws FileNotFoundException,IOException,NullPointerException
{

String name = " " ,usn=" ", sem = " ", branch =" ",r;
File file = new File("student.txt");
BufferedReader br = new BufferedReader(new FileReader("student.txt"));
File temp = new File("temp.txt");
PrintWriter pw = new PrintWriter(temp);

System.out.println("Enter usn to be modified");


String usn1=scan.nextLine();
while((r=br.readLine())!=null)
{
String[] result = r.split( "\\ |");
name=result[0];
usn=result[1];
sem=result[2];
branch=result[3];
if(usn.equals(usn1))
{

System.out.println("The details are : "+name+" " +usn+" " +sem+" "+branch);


System.out.println("Enter name, usn, sem and branch ");

String name11 = scan.nextLine();


String usn11 = scan.nextLine();
String sem11 = scan.nextLine();
String branch11 = scan.nextLine();
String b = name11 + "|" + usn11+ "|" +sem11+ "|" +branch11+ "|";
int len =b.length();
String s1= "*";//�-"
if(len<50)
{
for(int j=len;j<=50;j++)
b.concat(s1);
pw.println(b);
}
}
else
pw.print(r);
}

pw.flush();
pw.close();
br.close();
if(file.delete())
{
System.out.println("File deleted");
}
else
System.out.println("File not deleted");
if(!(temp.renameTo(file)))
System.out.println("Renaming is not done");
}
}

You might also like