快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

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

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

java手机通讯代码,java通信编程

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

具体方法如下:

创新互联-专业网站定制、快速模板网站建设、高性价比漾濞网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式漾濞网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖漾濞地区。费用合理售后完善,十年实体公司更值得信赖。

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即时通讯的一个简单功能代码

20分!!!??(⊙o⊙)

给你这个做参考吧。自己改一下就行了。(共两个文件)

//ChatClient.java

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

public class ChatClient extends Frame {

Socket s = null;

DataOutputStream dos = null;

DataInputStream dis = null;

private boolean bConnected = false;

TextField tfTxt = new TextField();

TextArea taContent = new TextArea();

Thread tRecv = new Thread(new RecvThread());

public static void main(String[] args) {

new ChatClient().launchFrame();

}

public void launchFrame() {

setLocation(400, 300);

this.setSize(300, 300);

add(tfTxt, BorderLayout.SOUTH);

add(taContent, BorderLayout.NORTH);

pack();

this.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent arg0) {

disconnect();

System.exit(0);

}

});

tfTxt.addActionListener(new TFListener());

setVisible(true);

connect();

tRecv.start();

}

public void connect() {

try {

s = new Socket("127.0.0.1", 8888);

dos = new DataOutputStream(s.getOutputStream());

dis = new DataInputStream(s.getInputStream());

System.out.println("connected!");

bConnected = true;

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public void disconnect() {

try {

dos.close();

dis.close();

s.close();

} catch (IOException e) {

e.printStackTrace();

}

/*

try {

bConnected = false;

tRecv.join();

} catch(InterruptedException e) {

e.printStackTrace();

} finally {

try {

dos.close();

dis.close();

s.close();

} catch (IOException e) {

e.printStackTrace();

}

}

*/

}

private class TFListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

String str = tfTxt.getText().trim();

//taContent.setText(str);

tfTxt.setText("");

try {

//System.out.println(s);

dos.writeUTF(str);

dos.flush();

//dos.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

private class RecvThread implements Runnable {

public void run() {

try {

while(bConnected) {

String str = dis.readUTF();

//System.out.println(str);

taContent.setText(taContent.getText() + str + '\n');

}

} catch (SocketException e) {

System.out.println("退出了,bye!");

} catch (EOFException e) {

System.out.println("推出了,bye - bye!");

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

//ChatServer.java

import java.io.*;

import java.net.*;

import java.util.*;

public class ChatServer {

boolean started = false;

ServerSocket ss = null;

ListClient clients = new ArrayListClient();

public static void main(String[] args) {

new ChatServer().start();

}

public void start() {

try {

ss = new ServerSocket(8888);

started = true;

} catch (BindException e) {

System.out.println("端口使用中....");

System.out.println("请关掉相关程序并重新运行服务器!");

System.exit(0);

} catch (IOException e) {

e.printStackTrace();

}

try {

while(started) {

Socket s = ss.accept();

Client c = new Client(s);

System.out.println("a client connected!");

new Thread(c).start();

clients.add(c);

//dis.close();

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

ss.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

class Client implements Runnable {

private Socket s;

private DataInputStream dis = null;

private DataOutputStream dos = null;

private boolean bConnected = false;

public Client(Socket s) {

this.s = s;

try {

dis = new DataInputStream(s.getInputStream());

dos = new DataOutputStream(s.getOutputStream());

bConnected = true;

} catch (IOException e) {

e.printStackTrace();

}

}

public void send(String str) {

try {

dos.writeUTF(str);

} catch (IOException e) {

clients.remove(this);

System.out.println("对方退出了!我从List里面去掉了!");

//e.printStackTrace();

}

}

public void run() {

try {

while(bConnected) {

String str = dis.readUTF();

System.out.println(str);

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

Client c = clients.get(i);

c.send(str);

//System.out.println(" a string send !");

}

/*

for(IteratorClient it = clients.iterator(); it.hasNext(); ) {

Client c = it.next();

c.send(str);

}

*/

/*

IteratorClient it = clients.iterator();

while(it.hasNext()) {

Client c = it.next();

c.send(str);

}

*/

}

} catch (EOFException e) {

System.out.println("Client closed!");

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if(dis != null) dis.close();

if(dos != null) dos.close();

if(s != null) {

s.close();

//s = null;

}

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

}

输入3位数字 在通讯录中找到跟前3位数字相匹配的手机号码,用java怎么写代码?

楼上应该放出答案了,我这里只是用其他界面显示,不懂Swing的可以略过,(代码比较粗略,不一定符合规范,照搬需谨慎)

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextField;

public class Demo extends JFrame {

ListString pnos = new ArrayListString();

public static void main(String[] args) {

new Demo();

}

public Demo() {

StringBuffer sb = new StringBuffer("htmlbody");

while (pnos.size()  15) {// "随机"生成电话号码,可以不理他

String telString = getRandomTel();

if (!pnos.contains(telString)) {

pnos.add(telString);

sb.append(telString);

if (pnos.size() % 3 == 0) {

sb.append("br/");

} else if (pnos.size()  15) {

sb.append(",");

}

}

}

sb.append("/body/html");

JLabel label = new JLabel(sb.toString());

final JTextField input = new JTextField();

final JLabel result = new JLabel();

this.setLayout(new GridLayout(3, 1));

this.add(label);

this.add(input);

this.add(result);

this.setSize(new Dimension(300, 500));

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setLocationRelativeTo(null);

this.setVisible(true);

input.addKeyListener(new KeyAdapter() {

@Override

public void keyReleased(KeyEvent keyevent) {

result.setText(getMatchNo(input.getText()));

}

});

}

/**

 * 

 * @param perfix

 * @return

 */

public String getMatchNo(String perfix) {

StringBuffer sb = new StringBuffer("htmlbody");

if (perfix != null  !perfix.trim().equals(""))

for (String string : pnos) {// 这里是你要的部分,遍历“号码本”匹配是否为输入数字的开头

if (string.startsWith(perfix)) {

sb.append(string).append("br/");

}

}

sb.append("/body/html");

return sb.toString();

}

private static String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153"

.split(",");

private static String getRandomTel() {

int index = getNum(0, telFirst.length - 1);

String first = telFirst[index];

String second = String.valueOf(getNum(1, 888) + 10000).substring(1);

String thrid = String.valueOf(getNum(1, 9100) + 10000).substring(1);

return first + second + thrid;

}

public static int getNum(int start, int end) {

return (int) (Math.random() * (end - start + 1) + start);

}

}

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语言写段简单,但又有技术含量的即时通讯代码,不胜感激之情溢于满天下

这里有一个简单的模拟通讯 要先运行服务器端 再运行客户端 否则会报错:

服务器端代码:

package com.test3;

import java.net.*;

import java.io.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Server2 extends JFrame implements ActionListener , KeyListener {

JTextArea jta=null;

JScrollPane jsp=null;

JTextField jtf=null;

JButton jb=null;

JPanel jp=null;

InputStreamReader isr=null;

BufferedReader br=null;

PrintWriter pw=null;

Socket s;

String jtatext="";

public static void main(String[] args) {

// TODO Auto-generated method stub

Server2 sv2=new Server2();

}

public Server2(){

jta=new JTextArea();

jta.setEditable(false);

jsp=new JScrollPane(jta);

jtf=new JTextField(10);

jtf.addKeyListener(this);

jb=new JButton("发送");

jb.addActionListener(this);

jp=new JPanel();

jp.add(jtf);

jp.add(jb);

this.add(jsp,"Center");

this.add(jp,"South");

this.setSize(300,300);

this.setLocationRelativeTo(this);

this.setTitle("服务器端");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

try{

ServerSocket ss=new ServerSocket(9999);

s=ss.accept();

isr=new InputStreamReader(s.getInputStream());

br=new BufferedReader(isr);

pw=new PrintWriter(s.getOutputStream(),true);

while(true){

String info=br.readLine();

jta.append("客户端对服务器说:   "+info+"\r\n");

// this.jta.setText(jtatext);

}

}catch(Exception e){

e.printStackTrace();

}

}

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource()==jb){

try {

pw.println(jtf.getText());

jta.append("服务器对客户端说:   "+jtf.getText()+"\r\n");

// jta.setText(jtatext);

jtf.setText("");

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

@Override

public void keyTyped(KeyEvent e) {}

@Override

public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_ENTER){

try {

pw.println(jtf.getText());

jta.append("服务器对客户端说:   "+jtf.getText()+"\r\n");

jtf.setText("");

} catch (Exception e1) {

e1.printStackTrace();

}

}

}

@Override

public void keyReleased(KeyEvent e) {}

}

客户端代码:

package com.test3;

import java.net.*;

import java.io.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Client2 extends JFrame implements ActionListener ,KeyListener {

JTextArea jta=null;

JScrollPane jsp=null;

JTextField jtf=null;

JButton jb=null;

JPanel jp=null;

String jtatext="";

Socket s;

PrintWriter pw=null;

InputStreamReader isr=null;

BufferedReader br=null;

public static void main(String[] args) {

// TODO Auto-generated method stub

Client2 sv2=new Client2();

}

public Client2(){

jta=new JTextArea();

jta.setEditable(false);

jsp=new JScrollPane(jta);

jtf=new JTextField(10);

jtf.addKeyListener(this);

jb=new JButton("发送");

jb.addActionListener(this);

jp=new JPanel();

jp.add(jtf);

jp.add(jb);

this.add(jsp,"Center");

this.add(jp,"South");

this.setSize(300,300);

this.setLocationRelativeTo(this);

this.setTitle("客户端");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

try {

 s=new Socket("127.3.3.3",9999);

 isr=new InputStreamReader(s.getInputStream());

 br=new BufferedReader(isr);

 pw=new PrintWriter(s.getOutputStream(),true);

 

 while(true){

 String info=br.readLine(); 

jta.append("服务器对客户端说:   "+info+"\r\n");

 

 }

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource()==jb){

try {

pw.println(this.jtf.getText());

jta.append("客户端对服务器说:   "+jtf.getText()+"\r\n");

jtf.setText("");

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

public void keyTyped(KeyEvent e) {}

public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_ENTER){

try {

pw.println(this.jtf.getText());

jta.append("客户端对服务器说:   "+jtf.getText()+"\r\n");

jtf.setText("");

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

public void keyReleased(KeyEvent e) {}

}


当前标题:java手机通讯代码,java通信编程
分享路径:http://6mz.cn/article/phsdgs.html

其他资讯