### 综合练习使用 Socket 和多线程编写一个简单的聊天小程序,要求客户端和服务端交替发送消息,在客户端和服务端都能看到彼此的聊天记录。Server```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class Server {public static void main(String[] args) {ServerSocket serverSocket = null;Socket socket = null;try {serverSocket = new ServerSocket(8080);System.out.println("服务端已启动");while(true) {socket = serverSocket.accept();new Thread(new SocketThread(socket)).start();;}} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}
}
```ServerThread```java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;public class SocketThread implements Runnable {private Socket socket;public SocketThread(Socket socket) {this.socket = socket;}@Overridepublic void run() {// TODO Auto-generated method stubInputStream inputStream = null;DataInputStream dataInputStream = null;Scanner scanner = new Scanner(System.in);String message = null;OutputStream outputStream = null;DataOutputStream dataOutputStream = null;try {while(true) {//读inputStream = socket.getInputStream();dataInputStream = new DataInputStream(inputStream);message = dataInputStream.readUTF();System.out.println("客户端:"+message);//写System.out.print("服务器:");message = scanner.next();outputStream = socket.getOutputStream();dataOutputStream = new DataOutputStream(outputStream);dataOutputStream.writeUTF(message);}} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}finally {try {dataOutputStream.close();outputStream.close();dataInputStream.close();inputStream.close();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}}
}
```Client```java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;public class Client {public static void main(String[] args) {Socket socket = null;InputStream inputStream = null;DataInputStream dataInputStream = null;OutputStream outputStream = null;DataOutputStream dataOutputStream = null;System.out.println("客户端已启动...");try {socket = new Socket("127.0.0.1", 8080);Scanner scanner = new Scanner(System.in);String message = null;while(true) {//写System.out.print("客户端:");message = scanner.next();outputStream = socket.getOutputStream();dataOutputStream = new DataOutputStream(outputStream);dataOutputStream.writeUTF(message);//读inputStream = socket.getInputStream();dataInputStream = new DataInputStream(inputStream);message = dataInputStream.readUTF();System.out.println("服务器:"+message);}} catch (UnknownHostException e) {// TODO Auto-generated catch block
e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}finally {try {dataInputStream.close();inputStream.close();dataOutputStream.close();outputStream.close();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}}
}
&#96;&#96;&#96;### XML 解析XML&#xff1a;Extensible Markup Language 可扩展标记语言特点&#xff1a;XML 与操作系统无关、与编程语言无关&#xff0c;可以实现不同系统之间的数据交换。XML 文档内容由一系列标签元素组成<标签名 属性名&#61;"属性值">元素内容标签名>- 属性值用双引号包裹
- 一个元素可以包含多个属性
- 属性值中不能直接包含<、>、"、&、&#39;
&#96;&#96;&#96;xml
&#96;&#96;&#96;注意事项- 所有 XML 元素都必须有结束标签
- XML 标签对大小写敏感
- XML 必须有正确的嵌套
- 同级标签以缩进对齐
- 元素名称可以包含字母、数字或其他字符
- 元素名称不能以数字或标点符号开头
- 元素名称不能包含空格XML 的转义字符特殊字符&#xff1a;<> " &#39; &
转义符&#xff1a;&#96;&#96;&#96;
<> " ' &
&#96;&#96;&#96;衣服型号 对应身高S <165M 165-170L 170-175XL 175-180XXL 180-185&#96;&#96;&#96;xml
&#96;&#96;&#96;考生姓名 预计得分 实际得分张三 75 80李四 75 88&#96;&#96;&#96;xml
&#96;&#96;&#96;### 解析XML用 Java 程序读取、操作 XML 文件中的数据dom4j 主流的 Java XML API&#xff0c;性能优异&#xff0c;功能强大。&#96;&#96;&#96;xml
&#96;&#96;&#96;&#96;&#96;&#96;java
import java.util.Iterator;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class Test {public static void main(String[] args) {SAXReader saxReader &#61; new SAXReader();try {Document document &#61; saxReader.read("src/phone.xml");//获取根节点Element root &#61; document.getRootElement();Iterator
e.printStackTrace();}}
}
&#96;&#96;&#96;&#96;&#96;&#96;java
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;public class Test2 {public static void main(String[] args) {SAXReader saxReader &#61; new SAXReader();try {Document document &#61; saxReader.read("src/phone.xml");Element root &#61; document.getRootElement();Element newEle &#61; root.addElement("brand");newEle.addAttribute("name", "Vivo");Element type &#61; newEle.addElement("type");type.addAttribute("name", "型号1");OutputFormat format &#61; OutputFormat.createPrettyPrint();format.setEncoding("UTF-8");Writer fileWriter &#61; new FileWriter("src/phone.xml");XMLWriter writer &#61; new XMLWriter(fileWriter,format);writer.write(document);writer.close();} catch (DocumentException e) {// TODO Auto-generated catch block
e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}
}
&#96;&#96;&#96;
Client.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;public class Client {public static void main(String[] args) {Socket socket &#61; null;InputStream inputStream &#61; null;DataInputStream dataInputStream &#61; null;OutputStream outputStream &#61; null;DataOutputStream dataOutputStream &#61; null;System.out.println("客户端已启动...");try {socket &#61; new Socket("127.0.0.1", 8080);Scanner scanner &#61; new Scanner(System.in);String message &#61; null;while(true) {//写System.out.print("客户端&#xff1a;");message &#61; scanner.next();outputStream &#61; socket.getOutputStream();dataOutputStream &#61; new DataOutputStream(outputStream);dataOutputStream.writeUTF(message);//读inputStream &#61; socket.getInputStream();dataInputStream &#61; new DataInputStream(inputStream);message &#61; dataInputStream.readUTF();System.out.println("服务器&#xff1a;"&#43;message);}} catch (UnknownHostException e) {// TODO Auto-generated catch block
e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}finally {try {dataInputStream.close();inputStream.close();dataOutputStream.close();outputStream.close();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}}
}
cloth.xml
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<cloth><size range&#61;"height>165">Ssize><size range&#61;"165
cloth>
phone.xml
xml version&#61;"1.0" encoding&#61;"UTF-8"?><phoneinfo id&#61;"1"> <brand name&#61;"华为"> <type name&#61;"U8650"/> <type name&#61;"HW123"/> <type name&#61;"HW321"/> brand> <brand name&#61;"苹果"> <type name&#61;"iPhone"/> brand> <brand name&#61;"三星"> <type name&#61;"Note4"/> brand> <brand name&#61;"Vivo"/> <brand name&#61;"Vivo"><type name&#61;"型号1"/>brand>
phoneinfo>
score.xml
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<students><student name&#61;"张三" expected_score&#61;"75" score&#61;"80">student><student name&#61;"李四" expected_score&#61;"75" score&#61;"88">student>
students>
Server.java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class Server {public static void main(String[] args) {ServerSocket serverSocket &#61; null;Socket socket &#61; null;try {serverSocket &#61; new ServerSocket(8080);System.out.println("服务端已启动");while(true) {socket &#61; serverSocket.accept();new Thread(new SocketThread(socket)).start();;}} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}
}
SocketThread.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;public class SocketThread implements Runnable {private Socket socket;public SocketThread(Socket socket) {this.socket &#61; socket;}&#64;Overridepublic void run() {// TODO Auto-generated method stubInputStream inputStream &#61; null;DataInputStream dataInputStream &#61; null;Scanner scanner &#61; new Scanner(System.in);String message &#61; null;OutputStream outputStream &#61; null;DataOutputStream dataOutputStream &#61; null;try {while(true) {//读inputStream &#61; socket.getInputStream();dataInputStream &#61; new DataInputStream(inputStream);message &#61; dataInputStream.readUTF();System.out.println("客户端&#xff1a;"&#43;message);//写System.out.print("服务器&#xff1a;");message &#61; scanner.next();outputStream &#61; socket.getOutputStream();dataOutputStream &#61; new DataOutputStream(outputStream);dataOutputStream.writeUTF(message);}} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}finally {try {dataOutputStream.close();outputStream.close();dataInputStream.close();inputStream.close();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}}}
Test.java
import java.util.Iterator;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class Test {public static void main(String[] args) {SAXReader saxReader &#61; new SAXReader();try {Document document &#61; saxReader.read("src/phone.xml");//获取根节点Element root &#61; document.getRootElement();Iterator
e.printStackTrace();}}
}
test.xml
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<books><book id&#61;"001"><author>张三author><name>Java高级编程name><description>Java是一门面向对象高级编程语言description>book><book id&#61;"002" author&#61;"李四" name&#61;"C#" description&#61;"面向对象">book>
books>
Test2.java
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;public class Test2 {public static void main(String[] args) {SAXReader saxReader &#61; new SAXReader();try {Document document &#61; saxReader.read("src/phone.xml");Element root &#61; document.getRootElement();Element newEle &#61; root.addElement("brand");newEle.addAttribute("name", "Vivo");Element type &#61; newEle.addElement("type");type.addAttribute("name", "型号1");OutputFormat format &#61; OutputFormat.createPrettyPrint();format.setEncoding("UTF-8");Writer fileWriter &#61; new FileWriter("src/phone.xml");XMLWriter writer &#61; new XMLWriter(fileWriter,format);writer.write(document);writer.close();} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}