从数据文件读取和写入的地址簿

时间:2011-12-10 21:07:26

标签: java addressbook

我正在做一个小程序地址簿,允许用户:添加联系人,搜索联系人和删除联系人。所有这些数据都被读取并写入 .dat文件

另外,如何在数据文件中创建布局(即姓名,姓氏,地址和号码)? 我在Java上很糟糕,我需要完成这项工作。

我的代码:

public interface Inter
{
    //Interface class 
    public void addContact();
    public void deleteContact();
    public void searchContact();
    public void readFile();
}

public class Contact
{
    static String name;
    static String lastName;
    static String address;
    static String number;

    public Contact () { }
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader; // reads from dat file
import java.io.FileWriter; // writes from dat file
import java.io.IOException;
import java.io.InputStreamReader; 

public class Phonebook extends Contact implements Inter
{
    public static void main(String[] args)
    {
    } // main

    @Override
    public void deleteContact() { }

    @Override
    public void searchContact() { }

    @Override
    public void addContact() 
    {
        String details = null;
        System.out.println("Enter new contact i.e name:number:lastname ");
        InputStreamReader converter = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(converter);

        try
        {
            details=in.readLine();
            String[] tokens =details.split(":"); // eg david :098:Needham
            name= tokens[0];
            lastName = tokens[1];
            address = tokens[2];
            number = tokens[3];
        }  catch (IOException e1) { }

        FileWriter fw = null; // writes contact info to the dat file
        try 
        {
            fw = new FileWriter("data.dat");
            fw.write(name);
            fw.write(lastName);
            fw.write(address);
            fw.write(number);
         } catch (IOException e) { }
         BufferedWriter bw = new BufferedWriter(fw);
    }

    public void readFile() // reads contacts from dat file
    {
        try
        {
            BufferedReader in = new BufferedReader(new FileReader("data.dat"));
            String str;
            while ((str = in.readLine()) != null) 
            {}
         } catch(Exception ex) { }
     }
}

1 个答案:

答案 0 :(得分:2)

您的文件格式应为.csv,因此看起来像:

name,lastname,address,number,
name,lastname,address,number,
name,lastname,address,number,

我知道我不应该为你发布代码,但是在这里:

class Contact {
  public String name, lastname, address, number;

  public Contact(String name, String lastname, String address, String number) {
    this.name = name;
    this.lastname = lastname;
    this.address = address;
    this.number = number;
  }

  public boolean equals(Contact c) {
    if(name.equals(c.name) && lastname.equals(c.lastname)
          && address.equals(c.address) && number.equals(c.number))
      return true;
    return false;
  }

  public String toString() {
    return name+","+lastname+","address+","+number+",";
  }
}

public class ContactDriver {
  public ArrayList<Contact> contacts = new ArrayList<Contact>();

  public static void addContact(Contact c) {
    contacts.add(c);
  }

  public static Contact deleteContact(Contact c) {
    return contacts.remove(c);
  }

  public static int searchContact(Contact c) {
    for(int i = 0; i < contacts.size(); i++)
      if(contacts.get(i).equals(c))
        return i;
    return -1;
  }

  public static void readContacts(String file) throws Exception {
    Scanner in = new Scanner(new File(file)).useDelimiter(",");

    while(in.hasNextLine()) {
      addContact(in.next(), in.next(), in.next(), in.next());
    }
  }

  public static void writeContacts(String fileName) {
    FileWriter dest = new FileWriter(fileName);

    for(Contact c : contacts)
      dest.write(c.toString());
  }

  public static void main() {
    readContacts();
    // Do logical stuffs
    writeContacts();
  }
}

该代码未经测试,因此我将编辑任何有错误的代码。

有趣学习更多Java!