学习java的第三十三天
作业:
1、Person类包括编号、姓名、年龄、性别属性。
创建多个Person对象,添加到集合中,将集合按年龄排序,
保存到文本文件中,格式如:
1 张三 20 男
2 李四 30 女
3 赵四 24 男
4 周五 40 女
2、在文件中保存一个有多个Person对象的集合,然后从文件中将集合读取出来,进行遍历。
3、编写简单的Socket消息传递过程,并理解流程。
Person类
package com.qf.home;
public class Person implements java.io.Serializable, Comparable<Object> {
private int id;//编号
private String name;//姓名
private int age;//年龄
private String sex;//性别
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int id, String name, int age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
@Override
public int compareTo(Object o) {
//0表示相同,只添加第一个
//1表示大,添加到右枝,即先添加的排列在前,顺序排列
//-1表示小,添加到左枝,即先添加的排列在后,倒序排列
if(o instanceof Person){
Person p = (Person)o;
if(p.age >= this.age){
return -1;
}else if(p.age < this.age){
return 1;
}
}
return 0;
}
}
Home01 测试类
package com.qf.home;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.TreeSet;
/**
* 1、Person类包括编号、姓名、年龄、性别属性。 创建多个Person对象,添加到集合中,将集合按年龄排序, 保存到文本文件中,
* 格式如:1 张三 20 男
* 2 李四 30 女
* 3 赵四 24 男
* 4 周五 40 女
* 2、在文件中保存一个有多个Person对象的集合,然后从文件中将集合读取出来,进行遍历。
*/
//对象流的Person类需实现Cloneable接口
public class Home01 {
public static void main(String[] args) {
// writeObject();
readObject();
}
public static void writeObject(){
// 写入
try {
FileOutputStream fos = new FileOutputStream("C:/Users/Administrator/Desktop/f.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Person p1 = new Person(1, "张三", 35, "男");
Person p2 = new Person(2, "李四", 33, "男");
Person p3 = new Person(3, "王五", 26, "男");
Person p4 = new Person(4, "赵六", 33, "男");
TreeSet<Person> treeSet = new TreeSet<>();
treeSet.add(p1);
treeSet.add(p2);
treeSet.add(p3);
treeSet.add(p4);
oos.writeObject(treeSet);
oos.flush();
fos.close();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readObject(){
//读取
try {
FileInputStream fis= new FileInputStream("C:/Users/Administrator/Desktop/f.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
TreeSet<Person> list =(TreeSet<Person>)ois.readObject();
list.forEach(System.out::println);
fis.close();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Socket实现聊天 (MyServer类 服务端)
package com.qf.demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class MyServer {
//服务端
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(7777);
while(true){
Socket socket = server.accept();
//得到与服务器的对话的流
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
byte [] buffer = new byte[1024];
int len = bis.read(buffer);
String s = new String(buffer,0,len);
System.out.println("客户端说:"+s);
Scanner input = new Scanner(System.in);
System.out.println("请输入内容:");
String s1 = input.next();
bos.write(s1.getBytes());
bos.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Socket实现聊天 (MyClient 类 客户类)
package com.qf.demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.net.Socket;
import java.util.Scanner;
public class MyClient {
//客户端
public static void main(String[] args) {
try {
while(true){
Socket socket = new Socket("192.168.50.129", 7777);
//得到与服务器的对话的流
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
Scanner input = new Scanner(System.in);
System.out.println("请输入内容:");
String s = input.next();
bos.write(s.getBytes());
bos.flush();
byte [] buffer = new byte[1024];
int len = bis.read(buffer);
String msg = new String(buffer,0,len);
System.out.println("服务器说:"+msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
io流 补全写入,保留原有的内容
package com.qf.demo;
import java.io.RandomAccessFile;
public class Demo01 {
public static void main(String[] args) {
try {
//随机流RandomAccessFile,既可以读文件也可以写文件;r:读,w:写,rw:可读可写
RandomAccessFile accessFile = new RandomAccessFile("a.txt", "rw");
//得到文件长度
long length = accessFile.length();
//表示在哪个地方开始写入文字
accessFile.seek(length);
accessFile.writeBytes("\n");//换行
accessFile.write("低头思故乡".getBytes());
accessFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
序列化
需要实现java.io.Serializable接口,分为两个过程,序列化和反序列化。
序列化是指使用何种规则将对象写入到文件中。
反序列化是指读取文件内容后,如何创建对象,并且封装对应的值到对象中。
序列化ID:serialVersionUID,只有相同的serialVersionUID的对象流才能相互读写。
transient:修饰属性,当使用此关键字时,序列化对象时会忽略掉该属性。
克隆 clone,对象流的复制或者拷贝的意思
步骤:
1. 类实现Cloneable接口。
2. 重写Object类中的clone方法。
3. 将protected关键字改成public,让类的外部可以访问。
浅拷贝 (实体类 Student,测试类Demo03)
实体类 Student
package com.qf.entity;
public class Student implements java.io.Serializable,Cloneable{
private static final long serialVersionUID = 6219950875927881633L;
private int id;
private String name;
private String sex;
private int age;
private transient String tel;
//使用transient修饰,序列化对象时会忽略掉该属性,得到值为null;
public Student() {
super();
}
public Student(int id, String name, String sex, int age, String tel) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.tel = tel;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", tel=" + tel + "]";
}
@Override
public Student clone() throws CloneNotSupportedException {
return (Student) super.clone();
}
}
测试类 Demo03
package com.qf.demo;
import com.qf.entity.Student;
public class Demo03 {
public static void main(String[] args) {
//克隆,clone
//1实现Cloneable接口
//2重写clone方法
//3用public修饰方法
Student s1 = new Student(1, "张三", "男", 25, "111111");
try {
Student s2 = s1.clone();
s2.setName("李四");
System.out.println(s1);
System.out.println(s2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
深拷贝 (实体类 Students,测试类Demo04)
实体类 Students
package com.qf.entity;
public class Students implements java.io.Serializable,Cloneable{
private static final long serialVersionUID = 6219950875927881633L;
private int id;
private String name;
private String sex;
private int age;
private transient String tel;
private Classes cla;
//使用transient修饰,序列化对象时会忽略掉该属性,得到值为null;
@Override
public Students clone() throws CloneNotSupportedException {
Students st = (Students) super.clone();
st.setCla(st.getCla().clone());
return st;
}
@Override
public String toString() {
return "Students [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", cla=" + cla + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Classes getCla() {
return cla;
}
public void setCla(Classes cla) {
this.cla = cla;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public Students(int id, String name, String sex, int age, String tel, Classes cla) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.tel = tel;
this.cla = cla;
}
public Students() {
super();
// TODO Auto-generated constructor stub
}
}
测试类Demo04
package com.qf.demo;
import com.qf.entity.Classes;
import com.qf.entity.Students;
public class Demo04 {
public static void main(String[] args) {
//克隆,clone
//1实现Cloneable接口
//2重写clone方法
//3用public修饰方法
Students st1 = new Students(1, "张三", "男", 25, "111111",new Classes(1,"java1904班"));
try {
Students st2 = st1.clone();
//等同于
Students st3 = new Students();
st3.setId(st1.getId());
st3.setName(st1.getName());
st3.setSex(st1.getSex());
st3.setAge(st1.getAge());
st3.setTel(st1.getTel());
Classes cla1 = new Classes();
cla1.setId(st1.getCla().getId());
cla1.setName(st1.getCla().getName());
st3.setCla(cla1);
st1.setName("李四");
st1.getCla().setName("java精英1904");
System.out.println(st1);
System.out.println(st2);
System.out.println(st3);
} catch (Exception e) {
e.printStackTrace();
}
}
}