CariDotMy

 Forgot password?
 Register

ADVERTISEMENT

View: 2902|Reply: 1

JAVA PROGRAMMING : PhoneBook

[Copy link]
Post time 13-10-2010 02:20 PM | Show all posts |Read mode
Salam semua,

mini projek nih tok sy punya kelas assignment. Lecture soh buat phonebook guna java dan fail input dia simpan dalam text file.
function dia pun setakat, add nama n no telefon, delete, edit dan view balik data yg dah simpan dalam textfile tuh td.

key-in data tuh pula melalui console (import java.util.Scanner).

sy ada try2 cari kat internet cth kebanyakkannya tak guna scanner + textfile.

anyone, tolong saya.
Reply

Use magic Report


ADVERTISEMENT


Post time 15-10-2010 03:28 AM | Show all posts
Post Last Edit by alusmetai at 15-10-2010 07:37

1. Tulis class Phonebook untuk represent the record.

2. Tulis class Scanner. refer kat website ini-> Java Scanner

3. Tulis class TextReader. Code aku amik dari internet je.

  1. public class TextReader {
  2.      private static void readFile(String fileName) {
  3.        try {
  4.          File file = new File(fileName);
  5.          FileReader reader = new FileReader(file);
  6.          BufferedReader in = new BufferedReader(reader);
  7.          String string;
  8.          while ((string = in.readLine()) != null) {
  9.            System.out.println(string);
  10.          }
  11.          in.close();
  12.        } catch (IOException e) {
  13.          e.printStackTrace();
  14.        }
  15.      }

  16.      public static void main(String[] args) {
  17.        if (args.length != 1) {
  18.          System.err.println("usage: java TextReader "
  19.            + "file location");
  20.          System.exit(0);
  21.        }
  22.        readFile(args[0]);
  23.      }
  24.    }
Copy the Code


4. Tambah method appendToFile() untuk tambah/edit record. Oleh sebab code readFile() di atas tu amik data dari 1 file sebaris demi sebaris, maka ko kenalah simpan setiap record/tuple dlm phonebook tu sebaris demi sebaris. Selepas tu ko kena parse kan lagi sekali tiap2 baris record tu utk dipecahkan kepada columns.

5. Untuk delete a particular record dlm file phonebook, sila rujuk code di bawah:
  1. public void removeLineFromFile(String file, String lineToRemove) {

  2.     try {

  3.       File inFile = new File(file);
  4.       
  5.       if (!inFile.isFile()) {
  6.         System.out.println("Parameter is not an existing file");
  7.         return;
  8.       }
  9.       
  10.       //Construct the new file that will later be renamed to the original filename.
  11.       File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
  12.       
  13.       BufferedReader br = new BufferedReader(new FileReader(file));
  14.       PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
  15.       
  16.       String line = null;

  17.       //Read from the original file and write to the new
  18.       //unless content matches data to be removed.
  19.       while ((line = br.readLine()) != null) {
  20.         
  21.         if (!line.trim().equals(lineToRemove)) {

  22.           pw.println(line);
  23.           pw.flush();
  24.         }
  25.       }
  26.       pw.close();
  27.       br.close();
  28.       
  29.       //Delete the original file
  30.       if (!inFile.delete()) {
  31.         System.out.println("Could not delete file");
  32.         return;
  33.       }
  34.       
  35.       //Rename the new file to the filename the original file had.
  36.       if (!tempFile.renameTo(inFile))
  37.         System.out.println("Could not rename file");
  38.       
  39.     }
  40.     catch (FileNotFoundException ex) {
  41.       ex.printStackTrace();
  42.     }
  43.     catch (IOException ex) {
  44.       ex.printStackTrace();
  45.     }
  46.   }
Copy the Code


Modify sikit method tu utk delete record berdasarkan input user contohnya user input Name dan bukan berdasarkan satu record. Of course di sini kita tak sentuh pasal database design issues spt user with same names apa nak buat etc etc.

notes: Kalau ko suka, ko boleh buat untuk tiap2 satu record ada satu file. Ni lagi senang. Jadi terpulanglah  kepada requirement atau pantang larang assignment ko atau selera ko sendiri utk pilih yg mana satu.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

 

ADVERTISEMENT



 

ADVERTISEMENT


 


ADVERTISEMENT
Follow Us

ADVERTISEMENT


Mobile|Archiver|Mobile*default|About Us|CariDotMy

9-1-2025 04:53 AM GMT+8 , Processed in 0.033746 second(s), 14 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

Quick Reply To Top Return to the list