我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java编写通讯录代码 用java程序编写个人通讯录

java通讯录管理系统设计代码怎么编译

java编写这个通讯录管理系统

创新互联建站长期为成百上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为阳朔企业提供专业的成都做网站、成都网站建设、成都外贸网站建设阳朔网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

java编写这个通讯录管理系统_Java如何实现通讯录管理系统

咕噜噜在芬兰

原创

关注

3点赞·2305人阅读

Java如何实现通讯录管理系统

发布时间:2020-07-28 09:39:42

来源:亿速云

阅读:65

作者:Leah

这篇文章将为大家详细讲解有关Java如何实现通讯录管理系统,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

本文实例为大家分享了java实现通讯录管理系统的具体代码,供大家参考,具体内容如下

完成项目的流程:

1.根据需求,确定大体方向

2.功能模块分析

3.界面实现

4.功能模块设计

5.coding

6.代码测试

下面是源代码:import java.awt.Container;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;

import java.util.concurrent.SynchronousQueue;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.WindowConstants;

import javax.swing.text.html.HTMLDocument.Iterator;

class Infro{

public String id;

public String name;

public String sex;

public String address;

public String e_mail;

public String phoneNumber;

static int index = 0;

static ArrayList list = new ArrayList();

static int len = list.size();

//构造函数

public Infro(String id,String name,String sex,String address,String e_mail,String phoneNumber){

this.id = id;

this.name = name;

this.sex = sex;

this.address = address;

this.e_mail = e_mail;

this.phoneNumber = phoneNumber;

}

public String toString(){

return "编号:"+id+" 姓名:"+name+" 性别:"+sex+" 通讯地址:"+address+" 邮箱地址:"+e_mail+" 电话:"+phoneNumber;

}

/**

* 添加功能

**/

public static void addFunction(){//添加功能

Infro infro = new Infro("","","","","","");

System.out.println("请输入添加的数据:");

Scanner in = new Scanner(System.in);

System.out.println("输入编号:");

infro.id = in.next();

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

infro.name = in.next();

System.out.println("输入性别:");

infro.sex = in.next();

System.out.println("输入通讯地址:");

infro.address = in.next();

System.

out.println("输入邮箱地址:");

infro.e_mail = in.next();

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

infro.phoneNumber = in.next();

list.add(index,infro);

index++;

if(list.isEmpty()){

System.out.println("数据添加失败啦");

}else{

System.out.println("数据添加成功啦");

len++;//list集合长度加一

// System.out.println(list.get(0).toString());

}

}

// public static void deleteFunction(){//删除功能

// System.out.println("输入要删除的联系人的编号");

// Scanner in_2 = new Scanner(System.in);

// String d1 = in_2.nextLine();

// for(int a= 0; a

// if(d1.equals(list.get(a).id)){

// list.remove(list.get(a));

// len --;

// }

// }

// }

/**

* 删除功能

**/

public static void deleteFunction(){

System.out.println("输入要删除的联系人的编号");

Scanner in_2 = new Scanner(System.in);

String d1 = in_2.nextLine();

java.util.Iterator it = list.iterator();

while (it.hasNext()){

Infro infro = it.next();

if (infro.id.equals(d1)){

it.remove();

--index;//一定要加这个,否则当做了删除操作再做添加操作的时候会出现异常(类似于指针,栈)

System.out.println("删除完毕"+"此时通讯录记录条数为:" + --len);

}

}

}

/**

* 修改功能

**/

public static void reditFunction(){

System.out.println("输入要修改的通讯录的Id");

Scanner in_r = new Scanner(System.in);

String r1 = in_r.nextLine();

for(int a = 0; a len;a++){

if(r1.equals(list.get(a).id)){

System.out.println("输入修改后的姓名:");

String name_1 = in_r.next();

list.get(a).name = name_1;

System.out.println("输入修改后的性别:");

String sex_1 = in_r.next();

list.get(a).sex = sex_1;

System.out.println("输入修改后的通讯地址:");

String address_1 = in_r.next();

list.get(a).address = address_1;

System.out.println("输入修改后的邮箱地址:");

String e_mail_1 = in_r.next();

list.get(a).e_mail = e_mail_1;

System.out.println("输入修改后的电话:");

String phoneNumber_1 = in_r.next();

list.get(a).phoneNumber = phoneNumber_1;

System.out.println("数据修改完毕");

}

}

}

/**

* 查询功能

**/

public static void searchFunction() throws Exception{//查询功能

System.out.println("请输入要查询的姓名:");

Scanner in_1 = new Scanner(System.in);

String s1=in_1.nextLine();

for(int a= 0; a

if(s1.equals(list.get(a).name)){

System.out.println(list.get(a).toString());

}

}

}

/**

* 显示功能

**/

public static void showFunction(){

for(int i = 0 ;i

System.out.println(list.get(i).toString());

}

}

/**

* 保存功能

**/

public static void writeFunction() throws IOException{

FileWriter writer = new FileWriter("通讯录管理.txt");

for(int i = 0 ;i

String []strwriter = new String[len];

strwriter[i]=list.get(i).toString();

writer.write(strwriter[i]);

writer.write("\r\n");

System.out.println("成功写入一行数据到 通讯录管理.txt 中");

}

writer.close();//关闭写入流,释放资源

}

/**

* 读取功能

**/

public static void readFunction() throws IOException{

FileReader reader = new FileReader("通讯录管理.txt");

BufferedReader br = new BufferedReader(reader);

String str;

while((str = br.readLine()) != null){//每次读取一行文本,判断是否到达文件尾

System.out.println(str);

}

br.close();

}

}

public class Demo extends JFrame {

/**

* 界面设计

**/

public Demo(){

Container c = getContentPane(); //定义一个顶级容器c

JPanel jp = new JPanel(); //新建JPanel面板--jp

JButton button1 = new JButton("新建联系人");

JButton button2 = new JButton("删除联系人");

JButton button3 = new JButton("编辑联系人");

JButton button4 = new JButton("查找联系人");

JButton button5 = new JButton("显示所有联系人");

JButton button6 = new JButton("保存联系人到本地");

JButton button7 = new JButton("读取本地联系人");

jp.setLayout(new GridLayout(2,4,5,5));//新建网格布局管理器(行数,列数,组件间的水平垂直间距)

jp.add(button1);

jp.add(button2);

jp.add(button3);

jp.add(button4);

jp.add(button5);

jp.add(button6);

jp.add(button7);

c.add(jp);//将JPanel面板jp添加到顶级容器c中

setSize(600,500);

setTitle("*通 讯 录 管 理 系 统*");

setVisible(true);

setResizable(false);//窗体大小由程序员决定,用户不能自由改变大小

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

/**

*按键响应

*

**/

button1.addActionListener(new ActionListener(){//添加功能实现

public void actionPerformed(ActionEvent arg0){

Infro.addFunction();

}

});

button2.addActionListener(new ActionListener(){//删除功能实现

public void actionPerformed(ActionEvent arg0){

Infro.deleteFunction();

}

});

button3.addActionListener(new ActionListener(){//修改功能实现

public void actionPerformed(ActionEvent arg0){

Infro.reditFunction();

}

});

button4.addActionListener(new ActionListener(){//查询功能实现

public void actionPerformed(ActionEvent arg0){

try {

Infro.searchFunction();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

button5.addActionListener(new ActionListener(){//显示功能实现

public void actionPerformed(ActionEvent arg0){

Infro.showFunction();

}

});

button6.addActionListener(new ActionListener(){//保存功能实现

public void actionPerformed(ActionEvent arg0){

try {

Infro.writeFunction();

} catch (IOException e) {

e.printStackTrace();

}

}

});

button7.addActionListener(new ActionListener(){//读取功能实现

public void actionPerformed(ActionEvent arg0){

try {

Infro.readFunction();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new Demo();

Infro a = new Infro("", "", "", "", "", "");

}

}

关于Java如何实现通讯录管理系统就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

求! 用JAVA编写一个通讯录,只要有最基本的添加、查找就可以

开发环境JBuilder2005 SQL server2003 SP3连接方士直链,如果直链不上改桥链,参考

数据文件代码:

create database userInfo

go

USE USERINFO

GO

create table uses(

username varchar(10) not null,

usertell varchar(20) not null,

usertells varchar(20) not null,

userQQ VARCHAR(20) NOT NULL,

USEREMAIL VARCHAR(20) NOT NULL,

USERADDRESS VARCHAR(20) NOT NULL

)

GO

Java部分代码:

文件一窗体控制类 文件名 Application1.java

package myjava;

import java.awt.Toolkit;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import java.awt.Dimension;

public class Application1 {

boolean packFrame = false;

/**

* Construct and show the application.

*/

public Application1() {

Frame1 frame = new Frame1();

// Validate frames that have preset sizes

// Pack frames that have useful preferred size info, e.g. from their layout

if (packFrame) {

frame.pack();

} else {

frame.validate();

}

// Center the window

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize = frame.getSize();

if (frameSize.height screenSize.height) {

frameSize.height = screenSize.height;

}

if (frameSize.width screenSize.width) {

frameSize.width = screenSize.width;

}

frame.setLocation((screenSize.width - frameSize.width) / 2,

(screenSize.height - frameSize.height) / 2);

frame.setVisible(true);

}

/**

* Application entry point.

*

* @param args String[]

*/

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

try {

UIManager.setLookAndFeel(UIManager.

getSystemLookAndFeelClassName());

} catch (Exception exception) {

exception.printStackTrace();

}

new Application1();

}

});

}

}

文件二 数据库驱动类文件名 DB.java

package myjava;

import java.sql.*;

/**

* pTitle: /p

*

* pDescription: /p

*

* pCopyright: Copyright (c) 2007/p

*

* pCompany: /p

*

* @author not attributable

* @version 1.0

*/

public class DB {

private String url = "jdbc:microsoft:sqlserver://";//SQLServer路径

private String serverName = "localhost";//服务器名称

private String portNumber = "1433";//端口号

private String databaseName = "userInfo";//数据库名称

private String userName = "sa";//登陆名

private String password = "sa";//密码

private Connection con = null;

private Statement st=null;

private ResultSet rs=null;

public DB(){

}

public void setUrl(String url){

this.url=url;

}

public String getUrl(){

return this.url;

}

public void setServerName(String serverName){

this.serverName=serverName;

}

public String getServerName(){

return this.serverName;

}

public void setPortNumber(String portNumber){

this.portNumber=portNumber;

}

public String getPortNumber(){

return this.portNumber;

}

public void setDatabaseName(String databaseName){

this.databaseName=databaseName;

}

public String getdatabaseName(){

return this.databaseName;

}

public void setUserName(String userName){

this.userName=userName;

}

public String getUserName(){

return this.userName;

}

public void setUserPassword(String password){

this.password=password;

}

public String getPassword(){

return this.password;

}

//返回连接url

private String getConnectionUrl(){

return url + serverName + ":" + portNumber + ";databaseName=" +

databaseName + ";";

}

//获得数据库联接驱动

public Connection getConnection(){

try {

//加载驱动

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

} catch (ClassNotFoundException ex) {

ex.printStackTrace();

return null;

}

try {

//获得驱动对象

con = DriverManager.

getConnection(getConnectionUrl(), userName,password);

} catch (SQLException ex1) {

ex1.printStackTrace();

return null;

}

return con;

}

//获得连接对象

public Statement getStatement(){

try {

st = getConnection().createStatement();

} catch (SQLException ex) {

ex.printStackTrace();

return null;

}

return st;

}

//获得结果集

public ResultSet getResultSet(String sql){

try {

rs = getStatement().executeQuery(sql);

} catch (SQLException ex) {

ex.printStackTrace();

return null;

}

return rs;

}

//更新数据库信息

public int getUpdate(String sql){

try {

return getStatement().executeUpdate(sql);

} catch (SQLException ex) {

ex.printStackTrace();

return 0;

}

}

//数据关闭驱动对象

public void close(){

try {

con.close();

} catch (SQLException ex) {

ex.printStackTrace();

}

}

//关闭连接对象

public void closeStatement(){

try {

if(st!=null){st.close();}

} catch (SQLException ex) {

ex.printStackTrace();

}

}

//关闭结果集

public void closeRestultSet(){

if(rs!=null){

try {

rs.close();

} catch (SQLException ex) {

}

}

}

}

文件三 封装用户信息类 文件名userInfo.java

package myjava;

public class userInfo {

private String uesrName;

private String userTell;

private String userTellS;

private String userQQ;

private String userEmail;

private String userAdderss;

public userInfo() {

}

public void setUesrName(String uesrName) {

this.uesrName = uesrName;

}

public void setUserTell(String userTell) {

this.userTell = userTell;

}

public void setUserTellS(String userTellS) {

this.userTellS = userTellS;

}

public void setUserQQ(String userQQ) {

this.userQQ = userQQ;

}

public void setUserEmail(String userEmail) {

this.userEmail = userEmail;

}

public void setUserAdderss(String userAdderss) {

this.userAdderss = userAdderss;

}

public String getUesrName() {

return uesrName;

}

public String getUserTell() {

return userTell;

}

public String getUserTellS() {

return userTellS;

}

public String getUserQQ() {

return userQQ;

}

public String getUserEmail() {

return userEmail;

}

public String getUserAdderss() {

return userAdderss;

}

}

文件四 操作控制类文件名 Frame1.java

package myjava;

import java.awt.*;

import javax.swing.*;

import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class Frame1 extends JFrame {

JPanel contentPane;

JLabel jLabel1 = new JLabel();

JLabel jLabel2 = new JLabel();

JLabel jLabel3 = new JLabel();

JLabel jLabel4 = new JLabel();

JLabel lblName = new JLabel();

JLabel jLabel6 = new JLabel();

JTextField txtTell = new JTextField();

JTextField txtTellS = new JTextField();

JTextField txtQQ = new JTextField();

JTextField txtEamil = new JTextField();

JTextField txtAddress = new JTextField();

JTextField txtName = new JTextField();

JButton jButton1 = new JButton();

JButton jButton2 = new JButton();

public Frame1() {

try {

setDefaultCloseOperation(EXIT_ON_CLOSE);

jbInit();

} catch (Exception exception) {

exception.printStackTrace();

}

}

/**

* Component initialization.

*

* @throws java.lang.Exception

*/

private void jbInit() throws Exception {

contentPane = (JPanel) getContentPane();

contentPane.setLayout(null);

setSize(new Dimension(400, 488));

setTitle("Frame Title");

jLabel1.setText("电话");

jLabel1.setBounds(new Rectangle(23, 83, 101, 39));

jLabel2.setText("手机");

jLabel2.setBounds(new Rectangle(23, 138, 101, 39));

jLabel3.setText("QQ");

jLabel3.setBounds(new Rectangle(23, 197, 101, 39));

jLabel4.setText("Email");

jLabel4.setBounds(new Rectangle(23, 261, 101, 39));

lblName.setText("姓名");

lblName.setBounds(new Rectangle(23, 33, 101, 39));

jLabel6.setText("地址");

jLabel6.setBounds(new Rectangle(23, 329, 101, 39));

txtTell.setBounds(new Rectangle(120, 89, 260, 33));

txtTellS.setBounds(new Rectangle(120, 146, 260, 33));

txtQQ.setBounds(new Rectangle(120, 199, 260, 33));

txtEamil.setSelectionStart(11);

txtEamil.setBounds(new Rectangle(120, 257, 260, 33));

txtAddress.setBounds(new Rectangle(120, 324, 260, 33));

txtName.setBounds(new Rectangle(120, 41, 260, 33));

jButton1.setBounds(new Rectangle(250, 405, 130, 36));

jButton1.setText("查找");

jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));

jButton2.setBounds(new Rectangle(29, 404, 130, 36));

jButton2.setText("添加");

jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));

contentPane.add(jLabel1);

contentPane.add(jLabel2);

contentPane.add(jLabel3);

contentPane.add(jLabel4);

contentPane.add(lblName);

contentPane.add(jLabel6);

contentPane.add(txtName);

contentPane.add(txtTell);

contentPane.add(txtTellS);

contentPane.add(txtQQ);

contentPane.add(txtEamil);

contentPane.add(txtAddress);

contentPane.add(jButton2);

contentPane.add(jButton1);

}

//判断输入的内容长度是否合法

private boolean Is(String str,int conut){

boolean is=false;

if(str==null||str.equals("")||str.length()conut){

is=true;

}

return is;

}

//判断是否为字符,不能含有空白字符

private boolean IsLetter(String str,int Count){

boolean is=false;

if(Is(str,Count)){

return is=true;

}

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

char ch=str.charAt(i);

if(!Character.isLetter(ch)){

return is=true;

}

}

return is=false;

}

//判断是否为数字,不能含有空白字符

private boolean IsNumber(String str,int conut){

boolean is=false;

if(Is(str,conut)){

return is=true;

}

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

char ch=str.charAt(i);

if(!Character.isDigit(ch)ch!='-'){

return is=true;

}

}

return is=false;

}

//判断邮箱是否合法

private boolean IsEmail(String Email,int count){

boolean is=false;

if(Is(Email,count)){

return is=true;

}

int index1=Email.indexOf("@");

int index2=Email.indexOf(".");

if(index1==-1||index2==-1||index1index2){

return is=true;

}

return is;

}

//获得用户信息对象

private userInfo getInfo(){

userInfo u=new userInfo();

String name=txtName.getText().trim();

String Tell=txtTell.getText().trim();

String TellS=txtTellS.getText().trim();

String QQ=txtQQ.getText().trim();

String Email=txtEamil.getText().trim();

String Address=txtAddress.getText().trim();

//判断姓名和地址

if(IsLetter(name,5)||IsLetter(Address,20)){

JOptionPane.showMessageDialog(this,"姓名或地址有误,数字填写只能含有'-'");

return null;

}

//判断邮箱是否合法

if(IsEmail(Email,16)){

JOptionPane.showMessageDialog(this,"邮箱填写有误!");

return null;

}

//判断联系方式中数字部分是否合法

if(IsNumber(Tell,12)||IsNumber(TellS,12)||IsNumber(QQ,12)){

JOptionPane.showMessageDialog(this,"填写联系方法有误,数字填写只能含有'-'");

return null;

}

u.setUesrName(name);

u.setUserTell(Tell);

u.setUserTellS(TellS);

u.setUserQQ(QQ);

u.setUserEmail(Email);

u.setUserAdderss(Address);

return u;

}

private int Insert(userInfo u){

DB D=new DB();

int i=0;

if(u!=null){

String sql="insert into uses values(?,?,?,?,?,?)";

try {

PreparedStatement ps = D.getConnection().prepareStatement(sql);

ps.setString(1,u.getUesrName());

ps.setString(2,u.getUserTell());

ps.setString(3,u.getUserTellS());

ps.setString(4,u.getUserQQ());

ps.setString(5,u.getUserEmail());

ps.setString(6,u.getUserAdderss());

i=ps.executeUpdate();

} catch (SQLException ex) {

ex.printStackTrace();

JOptionPane.showMessageDialog(this,"系统错误,添加失败!"+ex.getMessage());

return i;

}finally{

D.close();

}

}

return i;

}

private void getuserInfo(String userName) {

boolean is=false;

DB D=new DB();

if(IsLetter(userName,5)){

JOptionPane.showMessageDialog(this,"姓名不合法!");

return;

}

String sql="select * from uses where username=?";

try {

PreparedStatement ps = D.getConnection().prepareStatement(sql);

ps.setString(1,userName);

ResultSet rs=ps.executeQuery();

if(!rs.next()){

JOptionPane.showMessageDialog(this,"查询信息不存在!");

}else{

txtName.setText(rs.getString(1));

txtTell.setText(rs.getString(2));

txtTellS.setText(rs.getString(3));

txtQQ.setText(rs.getString(4));

txtEamil.setText(rs.getString(5));

txtAddress.setText(rs.getString(6));

}

} catch (SQLException ex) {

ex.printStackTrace();

JOptionPane.showMessageDialog(this,"系统错误!"+ex.getMessage());

return;

}finally{

D.close();

}

}

//添加按钮监听器

public void jButton2_actionPerformed(ActionEvent e) {

if(Insert(getInfo())==0){

JOptionPane.showMessageDialog(this,"添加信息失败!");

}

}

//查询按钮监听方法

public void jButton1_actionPerformed(ActionEvent e) {

getuserInfo(txtName.getText().trim());

}

}

class Frame1_jButton1_actionAdapter implements ActionListener {

private Frame1 adaptee;

Frame1_jButton1_actionAdapter(Frame1 adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton1_actionPerformed(e);

}

}

class Frame1_jButton2_actionAdapter implements ActionListener {

private Frame1 adaptee;

Frame1_jButton2_actionAdapter(Frame1 adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton2_actionPerformed(e);

}

}

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编写通讯录代码 用java程序编写个人通讯录
本文网址:http://mswzjz.cn/article/ddoohii.html

其他资讯