java通讯录关键代码,通讯录程序代码-成都快上网建站

java通讯录关键代码,通讯录程序代码

java 电子通讯录的载入和保存代码

思路如下:

创新互联公司专注于宁河网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供宁河营销型网站建设,宁河网站制作、宁河网页设计、宁河网站官网定制、微信小程序开发服务,打造宁河网络公司原创品牌,更为您提供宁河网站排名全网营销落地服务。

确定电子通讯录的文字格式,比如纯文本文件,每条通讯记录一行,每项内容之间使用tab键间隔

使用最基础的IO读写,操作此文件。

载入时从文件一行行读入,使用split拆分各项内容,存入一个对应的通讯录对象中,所有的通讯录对象保存在一个List中;

保存时,以覆盖的方式写入文件,以相同的格式写入即可。

用Java编辑通讯录

附件:源程序清单 import java.awt.*; import java.awt.event.*; class Info { public static String name,address,mail,telephone; Info(){} } public class addressBook extends Frame implements ActionListener { private MenuItem noteAdd=new MenuItem("Add"); private MenuItem noteDel=new MenuItem("Del"); private MenuItem noteEdit=new MenuItem("Modify"); private MenuItem noteExit=new MenuItem("Exit"); private List noteList=new List(); private String[][] message=new String [1000][4]; public Info notes=new Info(); public addressBook(){ super("通讯录 向阳"); Menu note=new Menu("Edit"); note.add(noteAdd); note.add(noteDel); note.add(noteEdit); note.addSeparator(); note.add(noteExit); MenuBar bar=new MenuBar(); bar.add(note); setMenuBar(bar); add(noteList); noteAdd.addActionListener(this); noteDel.addActionListener(this); noteEdit.addActionListener(this); noteExit.addActionListener(this); noteList.addActionListener(this); addWindowListener(new WindowCloser()); setSize(300,400); show(); } public void actionPerformed(ActionEvent e){ if(e.getSource()==noteAdd){ notes.mail=null; notes.name=null; notes.address=null; notes.telephone=null; int index=noteList.getItemCount(); EditDialog dlg=new EditDialog(this,"添加通讯录"); if(dlg.isOk){ message[index][0]=dlg.textName.getText(); message[index][1]=dlg.textAddress.getText(); message[index][2]=dlg.textEmail.getText(); message[index][3]=dlg.textTelephone.getText(); if (message[index][0]!=null){ String s="姓名:"+message[index][0]+";\t地址"+message[index][1]+";\tE-mail"+message[index][2]+";\t电话"+message[index][3]; noteList.add(s); } } dlg.dispose(); }else if(e.getSource()==noteDel){ ConfirmDialog dlg=new ConfirmDialog(this,"确认删除","确定要删除这一条吗?"); if (dlg.close){ noteList.remove(noteList.getSelectedIndex()); } dlg.dispose(); }else if(e.getSource()==noteEdit||e.getSource()==noteList){ int i=noteList.getSelectedIndex(); if(i!=-1){ notes.name=message[i][0]; notes.address=message[i][1]; notes.mail=message[i][2]; notes.telephone=message[i][3]; EditDialog dlg=new EditDialog(this,"修改通讯录"); if(dlg.isOk){ message[i][0]=dlg.textName.getText(); message[i][1]=dlg.textAddress.getText(); message[i][2]=dlg.textEmail.getText(); message[i][3]=dlg.textTelephone.getText(); String s="姓名:"+message[i][0]+";\t地址"+message[i][1]+";\tE-mail"+message[i][2]+";\t电话"+message[i][3]; noteList.replaceItem(s,i); } dlg.dispose(); } }else if(e.getSource()==noteExit){ System.exit(0); } } private class WindowCloser extends WindowAdapter{ public void WindowClosing(WindowEvent we){ System.exit(0); } } public static void main(String[]args){ addressBook a=new addressBook(); } } class EditDialog extends Dialog implements ActionListener { private Button ok=new Button("确定"); private Button cancel=new Button("取消"); private Label labelName=new Label("姓名",Label.LEFT); private Label labelAddress=new Label("地址",Label.LEFT); private Label labelEmail=new Label("E-mail",Label.LEFT); private Label labelTelephone=new Label("电话",Label.LEFT); TextField textName=new TextField(30); TextField textAddress=new TextField(30); TextField textEmail=new TextField(30); TextField textTelephone=new TextField(30); public boolean isOk=false; private Info notes=new Info(); public EditDialog(Frame parent,String title){ super(parent,title,true); Panel pName=new Panel(); pName.setLayout(new FlowLayout()); pName.add(labelName); pName.add(textName); Panel pAddress=new Panel(); pName.setLayout(new FlowLayout()); pName.add(labelAddress); pName.add(textAddress); Panel pEmail=new Panel(); pName.add(labelEmail); pName.add(textEmail); Panel pTelephone=new Panel(); pName.add(labelTelephone); pName.add(textTelephone); Panel pInfo=new Panel(); pInfo.setLayout(new GridLayout(4,1)); pInfo.add(pName); pInfo.add(pAddress); pInfo.add(pEmail); pInfo.add(pTelephone); Panel pButton=new Panel(); pButton.setLayout(new FlowLayout()); pButton.add(ok); pButton.add(cancel); setLayout(new BorderLayout()); add("Center",pInfo); add("South",pButton); textName.setText(notes.name); textAddress.setText(notes.address); textEmail.setText(notes.mail); textTelephone.setText(notes.telephone); ok.addActionListener(this); cancel.addActionListener(this); addWindowListener(new WindowCloser()); setResizable(false); pack(); show(); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==ok){ isOk=true; this.hide(); } if(ae.getSource()==cancel){ isOk=false; this.hide(); } } private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent we) { isOk=false; EditDialog.this.hide(); } } } class ConfirmDialog extends Dialog implements ActionListener { private Button okay=new Button("确定"); private Button cancel=new Button("取消"); private Label label=new Label(); public boolean close=false; public ConfirmDialog (Frame parent,String title,String question) { super(parent,title,true); label.setText(question); Panel buttons=new Panel(); buttons.setLayout(new FlowLayout()); buttons.add(okay); buttons.add(cancel); setLayout(new BorderLayout()); add("Center",label); add("South",buttons); okay.addActionListener(this); cancel.addActionListener(this); addWindowListener(new WindowCloser()); setResizable(false); pack(); show(); } private class WindowCloser extends WindowAdapter{ public void WindowClosing(WindowEvent we){ ConfirmDialog.this.close=false; ConfirmDialog.this.hide(); } public void actionPerformed(ActionEvent ae){ close=(ae.getSource()==okay); hide(); } }}

实现一个小型通讯录。Java

Friend类:public class Friend {

/*

* 姓名

*/

private String name;

/*

* 电话

*/

private String telephone;

/*

* 邮箱

*/

private String email;

/*

* 公司

*/

private String company; public String getName() {

return name;

} public void setName(String name) {

this.name = name;

} public String getTelephone() {

return telephone;

} public void setTelephone(String telephone) {

this.telephone = telephone;

} public String getEmail() {

return email;

} public void setEmail(String email) {

this.email = email;

} public String getCompany() {

return company;

} public void setCompany(String company) {

this.company = company;

} public String toString() {

StringBuffer str = new StringBuffer(); str.append("姓名:" + name).append("\n");

str.append("电话:" + telephone).append("\n");

str.append("邮箱:" + email).append("\n");

str.append("公司:" + company).append("\n");

str.append("-----------------------------------------\n");

return str.toString();

}

}AddFriend类:public class AddFriend { /**

* 主方法 程序的入口

*/

public static void main(String[] args) {

ListFriend friendList = new ArrayListFriend();

char isGo = 'Y';

int i = 0;

do {

Friend friend = new Friend();

System.out.println("请输入第" + (i + 1) + "位朋友的姓名:");

InputStreamReader reader = new InputStreamReader(System.in);

String str = "";

try {

str = (new BufferedReader(reader)).readLine();

} catch (IOException e) {

e.printStackTrace();

}

friend.setName(str); System.out.println("请输入第" + (i + 1) + "位朋友的电话:"); try {

str = (new BufferedReader(reader)).readLine();

} catch (IOException e) {

e.printStackTrace();

}

if (str.matches("\\d*") str.length() == 11) {// 判断用户输入的电话是否符合标准

friend.setTelephone(str);

} else {

System.out.println("电话号码输入有误,请重新输入!");

continue;

} System.out.println("请输入第" + (i + 1) + "位朋友的邮箱:"); try {

str = (new BufferedReader(reader)).readLine();

} catch (IOException e) {

e.printStackTrace();

}

friend.setEmail(str); System.out.println("请输入第" + (i + 1) + "位朋友的公司:"); try {

str = (new BufferedReader(reader)).readLine();

} catch (IOException e) {

e.printStackTrace();

}

friend.setCompany(str); friendList.add(friend); i++; System.out.println("是否继续添加?(Y/N):");

String go = "";

try {

go = (new BufferedReader(reader)).readLine();

} catch (IOException e) {

e.printStackTrace();

}

isGo = go.charAt(0);

} while (isGo == 'Y' || isGo == 'y'); for (int j = 0; j friendList.size(); j++) {

System.out.println(friendList.get(j).toString());

}

}

}

JAVA通讯录 求一个JAVA编写的通讯录,基本的就可以。

具体方法如下:

1、定义封装一条记录的实体类

2、根据实际系统容量,定义一个数组

3、完成系统中显示全部记录的逻辑

4、完成系统中添加一条记录的逻辑

5、完成系统中删除一条记录的逻辑

6、完成系统中修改一条记录的逻辑

7、全部代码:

import java.util.Scanner;

class Contact {

String cellPhone;

String name;

}

public class Main {

private static void menu () {

System.out.println("************** 菜单 ******"

+ "************");

System.out.println(" 1.显示全部通讯录");

System.out.println(" 2.增加一条记录");

System.out.println(" 3.删除一条记录");

System.out.println(" 4.修改一条记录");

System.out.println(" 0.退出");

}

public static void main(String[] args) {

Scanner scn = new Scanner(System.in);

Contact[] contacts = new Contact[200];

int size = 0;

String cmd = "";

do {

menu();

System.out.print("请输入你得选择:(0-4)");

cmd = scn.nextLine();

if (cmd.equals("1")) {

if (size == 0)

System.out.println("系统当前无记录!");

else

for (int i = 0; i size; i++) {

System.out.println(contacts[i].name + ":"

+ contacts[i].cellPhone);

}

} else if (cmd.equals("2")) {

System.out.print("请输入手机号:");

String cellphone = scn.nextLine();

System.out.print("请输入姓名:");

String name = scn.nextLine();

Contact contact = new Contact();

contact.cellPhone = cellphone;

contact.name = name;

if (size contacts.length) {

contacts[size++] = contact;

System.out.println("添加成功!");

} else {

System.out.println("你最多只能添加" +

contacts.length + "条记录");

}

} else if (cmd.equals("3")) {

System.out.print("请输入要删除的手机号:");

String cellphone = scn.nextLine();

int index = -1;

for (int i = 0; i size i contacts.length;

i++) {

if (contacts[i].cellPhone.equals(cellphone)) {

index = i;

break;

}

}

if (index == -1) {

System.out.println("该记录不存在!");

} else {

for (int i = index; i size; i++) {

contacts[index] = contacts[index + 1];

}

contacts[size - 1] = null;

size--;

System.out.println("删除成功!");

}

} else if (cmd.equals("4")) {

System.out.print("请输入要修改的手机号:");

String cellphone = scn.nextLine();

int index = -1;

for (int i = 0; i size i contacts.length;

i++) {

if (contacts[i].cellPhone.equals(cellphone)) {

index = i;

break;

}

}

if (index == -1) {

System.out.println("该记录不存在!");

} else {

System.out.print("请输入姓名:");

String name = scn.nextLine();

contacts[index].name = name;

}

}

} while (!cmd.equals("0"));

System.out.println("退出成功!");

scn.close();

System.exit(0);

}

}

java通讯录全部代码!

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

public class AddList {

private String filePath = "";

private String bakPath = "";

private String content = "";

Scanner sc = new Scanner(System.in);

public String readFile(){

content = "";

if (isNull(filePath)) {

System.out.println("文件存储路径:");

filePath = sc.nextLine();

}

File file = new File(filePath);

FileReader fr = null;

try {

if (file.exists()) {

fr = new FileReader(file);

char[] chars = new char[1024];

int n = 0;

while((n = fr.read(chars)) != -1){

String string = new String(chars, 0, n);

content = content + string;

}

} else {

System.out.println("文件不存在");

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (fr != null) {

try {

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return content;

}

public void writeFile(String path){

File file = new File(path);

FileOutputStream fos = null;

mkDirs(path);

try {

fos = new FileOutputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(fos);

PrintWriter pw = new PrintWriter(bos, true);

pw.print(content);

pw.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public void writeFile(){

if (isNull(filePath)) {

System.out.println("文件存储路径:");

filePath = sc.nextLine();

}

File file = new File(filePath);

FileOutputStream fos = null;

mkDirs(filePath);

try {

fos = new FileOutputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(fos);

PrintWriter pw = new PrintWriter(bos, true);

pw.print(content);

pw.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public void mkDirs(String filepath){

if (filepath.indexOf("\\") != -1) {

filepath = filepath.replaceAll("\\", "/");

}

int n = filepath.indexOf("//");

String path = filepath.substring(0, n) + "//";

filepath = filepath.substring(filepath.indexOf("//") + 1, filepath.length());

String[] files = filepath.split("/");

for (int i = 0; i files.length - 1; i++) {

path = path + files[i];

File file = new File(path);

if (!file.exists()) {

file.mkdir();

}

}

}

public void addImfor(){

System.out.println("--------增加记录---------");

String name = "";

String tel = "";

String email = "";

content = readFile();

while(true){

System.out.println("姓名:");

name = sc.next();

System.out.println("电话:");

tel = sc.next();

System.out.println("Email:");

email = sc.next();

content = content + name + "" + tel + "" + email +"==";

System.out.println("0、Exit 1、继续");

int i = sc.nextInt();

if (i == 0) {

break;

}

}

writeFile();

}

public void deleteImfor(){

System.out.println("---------删除记录---------");

String name = "";

String[] imfors = null;

content = readFile();

while(true){

System.out.println("你要删除的姓名是:");

name = sc.next();

if (content.indexOf(name) != -1) {

imfors = content.split("==");

for (int i = 0; i imfors.length; i++) {

if (imfors[i].indexOf(name) != -1) {

imfors[i] = "";

}

}


当前标题:java通讯录关键代码,通讯录程序代码
文章出自:http://kswjz.com/article/hcedjg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流