This code will help full for java beginners.
In this File creation and deletion methods are used.
import java.io.File;
import java.io.*;
import java.lang.String;
class Address
{
String name;
String addr;
int phone;
String Email;
String css;
public void addnew()
{
FileWriter fos;
File f1=new File("f1.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("enter the name");
name=br.readLine();
System.out.println("enter the address");
addr=br.readLine();
System.out.println("enter the phone number");
phone=Integer.parseInt(br.readLine());
System.out.println("enter the email");
Email=br.readLine();
fos= new FileWriter(f1, true);
fos.write(name + " | " + addr + " | " + Integer.toString(phone) + " | " + Email + "\n");
fos.close();
}
catch(IOException e)
{
name=null;
addr=null;
Email=null;
css=null;
}
}
public void delete()
{
int z;
String name;
try
{
BufferedReader bri=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the name which you want to delete");
name=bri.readLine();
File inFile = new File("f1.txt");
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(inFile));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
String[] temp;
temp = line.split(" | ");
if (!temp[0].equals(name)) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
System.out.println("deleted");
}
catch(Exception e)
{
System.out.println("error");
}
}
public void search()
{
String name;
try
{
BufferedReader bri=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter name which u want to search");
name=bri.readLine();
File file1 = new File("f1.txt");
BufferedReader br = new BufferedReader(new FileReader(file1));
String line = null;
while((line=br.readLine()) != null)
{
String[] temp;
temp=line.split(" | ");
if (temp[0].equals(name))
{
System.out.println("search is success");
System.out.println("name:" +temp[0]);
System.out.println("\naddress:" +temp[2]);
System.out.println("\nphone:" +temp[4]);
System.out.println("\nemail:" +temp[6]);
return;
}
}
}
catch(Exception e)
{
System.out.println("error");
}
}
public void View()
{
try
{
BufferedReader br=new BufferedReader(new FileReader("f1.txt"));
String line = null;
while((line=br.readLine()) != null)
{
System.out.println(line);
}
}
catch(Exception e)
{
System.out.println("error");
}
}
}
class AddressDemo
{
public static void main(String arg[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Address A=new Address();
String css;
try
{
do
{
System.out.println("which operation u want to perform");
System.out.println("1.addnew\n");
System.out.println("2.delete\n");
System.out.println("3.search\n");
System.out.println("4.view");
System.out.println("enter your choice");
int n=Integer.parseInt(br.readLine());
switch(n)
{
case 1:
A.addnew();
break;
case 2:
A.delete();
break;
case 3:
A.search();
break;
case 4:
A.View();
break;
default:
System.out.println("choose correct choice");
}
System.out.println("do u want to cntinue y/n");
css= br.readLine();
}
while(css.equals("y"));
}
catch(Exception e)
{
System.out.println("error is finding");
}
}
}
In this File creation and deletion methods are used.
import java.io.File;
import java.io.*;
import java.lang.String;
class Address
{
String name;
String addr;
int phone;
String Email;
String css;
public void addnew()
{
FileWriter fos;
File f1=new File("f1.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("enter the name");
name=br.readLine();
System.out.println("enter the address");
addr=br.readLine();
System.out.println("enter the phone number");
phone=Integer.parseInt(br.readLine());
System.out.println("enter the email");
Email=br.readLine();
fos= new FileWriter(f1, true);
fos.write(name + " | " + addr + " | " + Integer.toString(phone) + " | " + Email + "\n");
fos.close();
}
catch(IOException e)
{
name=null;
addr=null;
Email=null;
css=null;
}
}
public void delete()
{
int z;
String name;
try
{
BufferedReader bri=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the name which you want to delete");
name=bri.readLine();
File inFile = new File("f1.txt");
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(inFile));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
String[] temp;
temp = line.split(" | ");
if (!temp[0].equals(name)) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
System.out.println("deleted");
}
catch(Exception e)
{
System.out.println("error");
}
}
public void search()
{
String name;
try
{
BufferedReader bri=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter name which u want to search");
name=bri.readLine();
File file1 = new File("f1.txt");
BufferedReader br = new BufferedReader(new FileReader(file1));
String line = null;
while((line=br.readLine()) != null)
{
String[] temp;
temp=line.split(" | ");
if (temp[0].equals(name))
{
System.out.println("search is success");
System.out.println("name:" +temp[0]);
System.out.println("\naddress:" +temp[2]);
System.out.println("\nphone:" +temp[4]);
System.out.println("\nemail:" +temp[6]);
return;
}
}
}
catch(Exception e)
{
System.out.println("error");
}
}
public void View()
{
try
{
BufferedReader br=new BufferedReader(new FileReader("f1.txt"));
String line = null;
while((line=br.readLine()) != null)
{
System.out.println(line);
}
}
catch(Exception e)
{
System.out.println("error");
}
}
}
class AddressDemo
{
public static void main(String arg[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Address A=new Address();
String css;
try
{
do
{
System.out.println("which operation u want to perform");
System.out.println("1.addnew\n");
System.out.println("2.delete\n");
System.out.println("3.search\n");
System.out.println("4.view");
System.out.println("enter your choice");
int n=Integer.parseInt(br.readLine());
switch(n)
{
case 1:
A.addnew();
break;
case 2:
A.delete();
break;
case 3:
A.search();
break;
case 4:
A.View();
break;
default:
System.out.println("choose correct choice");
}
System.out.println("do u want to cntinue y/n");
css= br.readLine();
}
while(css.equals("y"));
}
catch(Exception e)
{
System.out.println("error is finding");
}
}
}
0 Comments On This Entry
← February 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 |
Tags
My Blog Links
Recent Entries
-
AddressBook in javaon Aug 26 2009 08:10 AM
Search My Blog
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



Leave Comment








|