You are on page 1of 1

CodedPlace

The Haven Of Programming Codes

Android C C++ CSS HTML5 Java JQuery PHP Visual Basic .Net Visual Basic 6.0

Simple CRUD (Add, Edit, Delete And View) In JAVA Using .TXT(Text)
File As Database

Archives

April 2017

March 2017

February 2017

January 2017

December 2016

 January 27, 2017  1 Comment  By Rivera

Simple CRUD (Add, Edit, Delete and View) in JAVA using


.TXT(text) le as database
What we are going to learn is at le manipulation by conducting Add, Edit, Delete and View record using
.TXT le which is our at le database. The data in our .TXT(text) le should be arrange in CSV (Comma
Separated Value).

1 10001,Naldrix Gwapo,23,Bacolod city

In at le database, there is no column or elds to identify every data in a record but the data is imaginarily
identi ed (e.g. the above record).

1 ID,Name of Employee,Age,Address

Now, we will start our CRUD in java by creating rst our menu to navigate in add record module, edit
record module, view all record module, search record module and delete record module.

1         Scanner strInput = new Scanner(System.in);


2         String choice,cont = "y";        
3         
4         while( cont.equalsIgnoreCase("y") ) {        
5             System.out.println("\t\t Employee Information System\n\n");
6         
7         System.out.println("1 ===> Add New Employee Record ");
8         System.out.println("2 ===> View All Employee Record ");
9         System.out.println("3 ===> Delete Employee Record ");
10         System.out.println("4 ===> Search Specific Record ");
11         System.out.println("5 ===> Update Specific Record ");         
12     
13         System.out.print("\n\n");
14         System.out.println("Enter your choice: ");
15         choice = strInput.nextLine();
16         
17         if( choice.equals("1") ) {
18          naldrixClass.AddRecord();
19         } else if( choice.equals("2") ) {
20          naldrixClass.ViewAllRecord();
21         } else if( choice.equals("3") ) {
22          naldrixClass.DeleteRecordByID();
23         } else if( choice.equals("4") ) {
24          naldrixClass.SearchRecordbyID();
25         } else if( choice.equals("5") ) {
26          naldrixClass.updateRecordbyID();
27         }
28         
29         System.out.println("Do you want to continue? Y/N");
30         cont = strInput.nextLine();
31       
32         }

Create setter method for our add record and put the following code to add record in .TXT le.

1   public void AddRecord() throws IOException {


2     
3      BufferedWriter bw = new BufferedWriter( new FileWriter("naldrix_db.txt",true) );
4      Scanner strInput = new Scanner(System.in);
5     
6      String ID, name, age, addr;
7     
8      System.out.print("Enter the Employee ID: ");
9      ID = strInput.nextLine();
10      System.out.print("Enter the Employee Name: ");
11      name = strInput.nextLine();
12      System.out.print("Enter the Employee Age: ");
13      age = strInput.nextLine();
14      System.out.print("Enter the Employee Address: ");
15      addr = strInput.nextLine();    
16        
17      bw.write(ID+","+name+","+age+","+addr);
18      bw.flush();
19      bw.newLine();
20      bw.close();
21     
22     }

Create setter method for our view record and put the following code to view record from .TXT File.

1 public void ViewAllRecord() throws IOException {


2      BufferedReader br = new BufferedReader( new FileReader("naldrix_db.txt") );
3     
4      String record;
5     
6      System.out.println(" ------------------------------------------------------------- ");
7      System.out.println("| ID Name Age Address   |");
8      System.out.println(" ------------------------------------------------------------- ");
9     
10      while( ( record = br.readLine() ) != null ) {
11     
12      StringTokenizer st = new StringTokenizer(record,",");
13     
14      System.out.println("| "+st.nextToken()+" "+st.nextToken()+" "+st.nextToken()+"
15
16      }
17     
18      System.out.println("|                                                        |");
19      System.out.println(" ------------------------------------------------------------- ");
20      br.close();    
21     
22     }

Create setter method for our delete record and put the following code to delete record from .TXT File.

1 public void DeleteRecordByID() throws IOException {


2      Scanner strInput =  new Scanner(System.in);
3      String ID, record;
4     
5     
6      File tempDB = new File("naldrix_db_temp.txt");
7      File db = new File("naldrix_db.txt");
8     
9     
10      BufferedReader br = new BufferedReader( new FileReader( db ) );
11      BufferedWriter bw = new BufferedWriter( new FileWriter( tempDB ) );
12     
13     
14      System.out.println("\t\t Delete Employee Record\n");
15     
16      System.out.println("Enter the Employee ID: ");
17      ID =  strInput.nextLine();
18     
19     
20      while( ( record = br.readLine() ) != null ) {
21     
22     
23      if( record.contains(ID) )
24      continue;
25   
26      bw.write(record);
27      bw.flush();
28      bw.newLine();
29
30      }
31     
32      br.close();
33      bw.close();
34     
35      db.delete();
36     
37      tempDB.renameTo(db);
38  
39     }

Create setter method for our search record and put the following code to search record from .TXT File.

1 public void SearchRecordbyID() throws IOException {


2      String ID,record;
3      Scanner strInput = new Scanner(System.in);
4     
5      BufferedReader br = new BufferedReader( new FileReader("naldrix_db.txt") );
6     
7      System.out.println("\t\t Search Employee Record\n");
8     
9     
10      System.out.println("Enter the Employee ID: ");
11      ID = strInput.nextLine();
12     
13      System.out.println(" ------------------------------------------------------------- ");
14      System.out.println("| ID Name Age Address   |");
15      System.out.println(" ------------------------------------------------------------- ");
16     
17      while( ( record = br.readLine() ) != null ) {
18     
19      StringTokenizer st = new StringTokenizer(record,",");
20      if( record.contains(ID) ) {
21      System.out.println("| "+st.nextToken()+" "+st.nextToken()+" "+st.nextTo
22      }
23     
24     
25     
26      }
27     
28      System.out.println("|                                                        |");
29      System.out.println(" ------------------------------------------------------------- ");
30     
31      br.close();
32     
33     
34     
35     }

Create setter method for our update record and put the following code to update record from .TXT File.

1 public void updateRecordbyID() throws IOException {


2      String newName, newAge, newAddr, record, ID,record2;
3     
4      File db = new File("naldrix_db.txt");
5      File tempDB = new File("naldrix_db_temp.txt");
6     
7      BufferedReader br = new BufferedReader( new FileReader(db) );
8      BufferedWriter bw = new BufferedWriter( new FileWriter(tempDB) );
9          
10      Scanner strInput = new Scanner(System.in);
11     
12      System.out.println("\t\t Update Employee Record\n\n");  
13 /****/
14 System.out.println("Enter the Employee ID: ");
15      ID = strInput.nextLine();     
16      System.out.println(" -------------------------------------------------------------
17      System.out.println("| ID Name Age Address   |
18      System.out.println(" -------------------------------------------------------------
19     
20      while( ( record = br.readLine() ) != null ) {
21     
22      StringTokenizer st = new StringTokenizer(record,",");
23      if( record.contains(ID) ) {
24      System.out.println("| "+st.nextToken()+" "+st.nextToken()+" "+st.ne
25      }
26     
27      }     
28      System.out.println("|                                                        |
29      System.out.println(" -------------------------------------------------------------
30     
31      br.close();
32 /****/       
33      System.out.println("Enter the new Name: ");
34      newName = strInput.nextLine();    
35      System.out.println("Enter the new Age: ");
36      newAge = strInput.nextLine();  
37      System.out.println("Enter the new Address: ");
38      newAddr = strInput.nextLine();  
39     
40      BufferedReader br2 = new BufferedReader( new FileReader(db) );
41     
42      while( (record2 = br2.readLine() ) != null ) {    
43      if(record2.contains(ID)) {
44      bw.write(ID+","+newName+","+newAge+","+newAddr);
45      } else {
46     
47      bw.write(record2);
48      }    
49      bw.flush();
50      bw.newLine();
51      }
52     
53      bw.close();
54      br2.close();    
55      db.delete();    
56      boolean success = tempDB.renameTo(db);    
57      System.out.println(success);    
58     
59     }

Sample screenshots of the Simple CRUD in Java using .TXT le as at le database.

CRUD Menu screenshot

Simple CRUD (Add, Edit, Delete and View) in JAVA using .TXT(text) le as database

Add Record Module screenshot

View Record Module screenshot

Happy Coding.

Powered by WordPress. Theme: bizstart by: Design By Freehtmldesigns

You might also like