网站首页 > 文章中心 > 其它

java简易通讯录源代码

作者:小编 更新时间:2023-09-27 07:32:00 浏览量:27人看过

实现一个小型通讯录.Java

Friend类:public class Friend {

/*

java简易通讯录源代码-图1

* 姓名

*/

private String name;

* 电话

private String telephone;

* 邮箱

private String email;

* 公司

private String company; public String getName() {

java简易通讯录源代码-图2

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 {

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

friend.setTelephone(str);

} else {

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

continue;

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

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

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

String go = "";

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

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通讯录全部代码!

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;

if (file.exists()) {

fr = new FileReader(file);

int n = 0;

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

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

content = content + string;

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

} catch (Exception e) {

} finally {

if (fr != null) {

fr.close();

return content;

public void writeFile(String path){

File file = new File(path);

FileOutputStream fos = null;

mkDirs(path);

fos = new FileOutputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(fos);

PrintWriter pw = new PrintWriter(bos, true);

pw.print(content);

pw.flush();

} catch (FileNotFoundException e) {

if (fos != null) {

fos.close();

public void writeFile(){

mkDirs(filePath);

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];

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[] imfors = null;

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

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通讯录 求一个JAVA编写的通讯录,基本的就可以.

具体方法如下:

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

class Contact {

String cellPhone;

String name;

public class Main {

private static void menu () {

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

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

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

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

Scanner scn = new Scanner(System.in);

int size = 0;

String cmd = "";

menu();

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);

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) {

java简易通讯录源代码-图3

contacts[size++] = contact;

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

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

contacts.length + "条记录");

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

int index = -1;

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

i++) {

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

index = i;

if (index == -1) {

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

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

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

contacts[size - 1] = null;

size--;

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

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

contacts[index].name = name;

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

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

scn.close();

System.exit(0);

以上就是土嘎嘎小编为大家整理的java简易通讯录源代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章