Java综合练习题—TCP通信协议+xml操作+序列化反序列化综合题
TCP通信协议+xml操作+序列化反序列化综合体
需求:
<?xml version="1.0" encoding="GBK"?>
<citys>
<city id='010'>
<cityname>北京</cityname>
<cityarea>华北</cityarea>
<population>2114.8万人</population>
</city>
<city id='021'>
<cityname>上海</cityname>
<cityarea>华东</cityarea>
<population>2,500万人</population>
</city>
<city id='020'>
<cityname>广州</cityname>
<cityarea>华南</cityarea>
<population>1292.68万人</population>
</city>
<city id='028'>
<cityname>成都</cityname>
<cityarea>华西</cityarea>
<population>1417万人</population>
</city>
</citys>
(1)使用dom4j将信息存入xml中
(2)读取信息,并打印控制台
(3)添加一个city节点与子节点
(4)使用socket TCP协议编写服务端与客户端,
客户端输入城市ID,服务器响应相应城市信息
(5)使用socket TCP协议编写服务端与客户端,
客户端要求用户输入city对象,服务端接收并使用dom4j保存至XML文件
(1)–(3)问
//(1)--(3)问
public class demo1 {
public static void main(String[] args) throws IOException, DocumentException {
//(1)使用dom4j将信息存入xml中
saveXml();
//(3)添加一个city节点与子节点
// editXml();
// (2)读取信息,并打印控制台
// show();
}
// (2)读取信息,并打印控制台
public static void show() throws DocumentException {
// 获取原有的文档对象
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("code_hz03\\src\\ch010\\test03\\city.xml");
// 获取根标签citys
Element rootElement = document.getRootElement();
String name = rootElement.getName();
System.out.println(name);
List<Element> elements = rootElement.elements();
for (Element element : elements) {
System.out.println("=================");
// element:表示每一个city
// 获取根标签citys下的每一个city的名字
String name1 = element.getName();
System.out.println(name1);
// 获取每一个city的属性值
String idText = element.attribute("id").getText();
System.out.println(idText);
// 获取city标签下的每一个子标签
List<Element> elements1 = element.elements();
for (Element element1 : elements1) {
// element1:表示每一个city标签下的每一个子标签
System.out.println(element1.getName());
String text = element1.getText();
System.out.println(text);
}
}
}
// (3)添加一个city节点与子节点
public static void editXml() throws DocumentException, IOException {
// 读取原有文档
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("code_hz03\\src\\ch010\\test03\\city.xml");
// 获得根标签
Element rootElement = document.getRootElement();
Element city = rootElement.addElement("city");
// 设置新增city标签的属性值
city.addAttribute("id","005");
// 再给新增city标签添加子标签
city.addElement("cityname").setText("郑州");
// 再document对象重写写入
XMLWriter xmlWriter = new XMLWriter(
new FileWriter("code_hz03\\src\\ch010\\test03\\city.xml")
);
xmlWriter.write(document);
// 释放资源
xmlWriter.close();
}
// (1)使用dom4j将信息存入xml中
public static void saveXml() throws IOException {
// 获取一个空的document对象
Document document = DocumentHelper.createDocument();
// 添加根节点citys
Element citys = document.addElement("citys");
// 添加根节点citys下的子节点city
Element city1 = citys.addElement("city");
// 添加city节点下的节点
city1.addAttribute("id","001");
city1.addElement("cityname").setText("北京");
city1.addElement("cityarea").setText("华北");
city1.addElement("population").setText("2114.8万人");
// 添加根节点citys下的子节点city
Element city2 = citys.addElement("city");
// 添加city节点下的节点
city2.addAttribute("id","012");
city2.addElement("cityname").setText("上海");
city2.addElement("cityarea").setText("华东");
city2.addElement("population").setText("2114.8万人");
// 添加根节点citys下的子节点city
Element city3 = citys.addElement("city");
// 添加city节点下的节点
city3.addAttribute("id","003");
city3.addElement("cityname").setText("广州");
city3.addElement("cityarea").setText("华南");
city3.addElement("population").setText("2114.8万人");
// 添加根节点citys下的子节点city
Element city4 = citys.addElement("city");
// 添加city节点下的节点
city4.addAttribute("id","004");
city4.addElement("cityname").setText("成都");
city4.addElement("cityarea").setText("华西");
city4.addElement("population").setText("2114.8万人");
// 写出xml文件
XMLWriter xmlWriter =
new XMLWriter(new FileWriter("code_hz03\\src\\ch010\\test03\\city.xml"));
xmlWriter.write(document);
// 释放资源
xmlWriter.close();
}
}
第(4)问
使用socket TCP协议编写服务端与客户端,
客户端输入城市ID,服务器响应相应城市信息
public class CityClient {
public static void main(String[] args) throws IOException {
/*
使用socket TCP协议编写服务端与客户端,
客户端输入城市ID,服务器响应相应城市信息
*/
Scanner sc = new Scanner(System.in);
// 创建客户端连接对象
Socket socket = new Socket("127.0.0.1",10011);
// 获取字节输出流
OutputStream os = socket.getOutputStream();
// 输入城市id
System.out.print("请输入城市id:");
String id = sc.nextLine();
// 写出
os.write(id.getBytes());
// 输出结束标记
socket.shutdownOutput();
// 服务器端若采取第一种方式读,需要在客户端 os.close();
// os.close();
// 接收服务器端返回的城市信息
InputStream is = socket.getInputStream();
byte[] bytes = new byte[1024];
is.read(bytes);
String msg = new String(bytes);
System.out.println("msg:"+msg);
}
}
========================================================
public class CityServer {
public static void main(String[] args) throws IOException, DocumentException {
/*
使用socket TCP协议编写服务端与客户端,
客户端输入城市ID,服务器响应相应城市信息
*/
// 创建服务器端连接对象
ServerSocket ss = new ServerSocket(10011);
// 监听客户端连接
Socket socket = ss.accept();
// 获取字节输入流
InputStream is = socket.getInputStream();
//第一种读入方式,循环读取
// int len;
// while ((len = is.read()) != -1){
// System.out.print((char) len);
// }
// 第二种读入方式,一次读取多个
// 定义字节数组,存储读入数据
byte[] bytes = new byte[1024];
is.read(bytes);
// 数据来自客户端输入,将读入的数据转成字符串
String id = new String(bytes).trim();
System.out.println("id:"+id);
// 输入结束标记
socket.isInputShutdown();
// 根据传入不同的id的展示不同的城市信息
String cityMsg = idShow(id);
// 将城市信息返回给客户端
OutputStream os = socket.getOutputStream();
os.write(cityMsg.getBytes());
}
// 读取xml文件
public static String idShow(String id) throws DocumentException {
// 获取解析器读取xml文件
SAXReader saxReader = new SAXReader();
// 获取xml文档对象
Document document = saxReader.read("code_hz03\\src\\ch010\\test03\\city.xml");
// 获取根标签citys
Element rootElement = document.getRootElement();
// 获取根标签citys下的子标签city
List<Element> elements = rootElement.elements();
String msg = null;
// element:表示每一个city
for (Element element : elements) {
String cityId = element.attributeValue("id");
if(id.equals(cityId)){
msg +="id:" +cityId;
msg += "cityname:" + element.elementText("cityname");
msg += "cityarea:" + element.elementText("cityarea");
msg += "population:" + element.elementText("population");
}
return msg;
}
return null;
}
}
第(5)问
使用socket TCP协议编写服务端与客户端,
客户端要求用户输入city对象,服务端接收并使用dom4j保存至XML文件
public class City implements Serializable {
private String id;
private String cityname;
private String cityarea;
private String population;
public City() {
}
public City(String id, String cityname, String cityarea, String population) {
this.id = id;
this.cityname = cityname;
this.cityarea = cityarea;
this.population = population;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCityname() {
return cityname;
}
public void setCityname(String cityname) {
this.cityname = cityname;
}
public String getCityarea() {
return cityarea;
}
public void setCityarea(String cityarea) {
this.cityarea = cityarea;
}
public String getPopulation() {
return population;
}
public void setPopulation(String population) {
this.population = population;
}
@Override
public String toString() {
return "City{" +
"id='" + id + '\'' +
", cityname='" + cityname + '\'' +
", cityarea='" + cityarea + '\'' +
", population='" + population + '\'' +
'}';
}
}
=======================================================
public class CityClient {
public static void main(String[] args) {
/*
(5)使用socket TCP协议编写服务端与客户端,
客户端要求用户输入city对象,服务端接收并使用dom4j保存至XML文件
*/
System.out.println("======客户端=====");
Scanner sc = new Scanner(System.in);
try {
// 创建客户端连接对象
Socket socket = new Socket("127.0.0.1",10011);
// 获取字节输出流
OutputStream os = socket.getOutputStream();
// 创建城市对象
City city = new City();
System.out.print("请输入城市id:");
city.setId(sc.next());
System.out.print("请输入城市名字:");
city.setCityname(sc.next());
System.out.print("请输入城市所在区域:");
city.setCityarea(sc.next());
System.out.print("请输入城市人口:");
city.setPopulation(sc.next()+"万人");
// 序列化
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(city);
} catch (Exception e) {
e.printStackTrace();
}
}
}
=========================================================
public class CityServer {
public static void main(String[] args) {
/*
(5)使用socket TCP协议编写服务端与客户端,
客户端要求用户输入city对象,服务端接收并使用dom4j保存至XML文件
*/
System.out.println("服务器端");
try {
ServerSocket ss = new ServerSocket(10011);
// 监听客户端连接
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
// 反序列化
ObjectInputStream ois = new ObjectInputStream(is);
// 读入反序列化的对象
City city = (City) ois.readObject();
// 接收对象写入返回的信息
//将对象信息写入xml文件中
String msg = msg(city);
System.out.println(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
// 定义方法,将对象信息写入xml文件中
public static String msg(City city){
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("code_hz03\\src\\ch010\\test03\\city.xml");
// 获取根标签
Element rootElement = document.getRootElement();
// 给根标签添加子标签city
Element city1 = rootElement.addElement("city");
// 给city标签添加子标签,并设置文本值,属性值
city1.addAttribute("id",city.getId());
city1.addElement("cityname").setText(city.getCityname());
city1.addElement("cityarea").setText(city.getCityarea());
city1.addElement("population").setText(city.getPopulation());
// 将document写入
XMLWriter xmlWriter = new XMLWriter(
new FileWriter("code_hz03\\src\\ch010\\test03\\city.xml")
);
xmlWriter.write(document);
xmlWriter.close();
return "新增成功";
} catch (Exception e) {
e.printStackTrace();
}
return "新增失败";
}
}