1.什么是序列化机制?
序列化:将内存中的java对象转化成与平台无关的二进制流,将其持久化到磁盘中或者将其网络传输到另一个节点
反序列化:序列化的逆过程,将磁盘中的存储的java对象或者网络中传输的java对象转化成内存中的java对象。
2.如何实现序列化
①实现Serializable接口
②设置序列版本号
③属性也是可序列化的
注意细节:
①static、transient修饰的属性不可序列化
②若没有显示的设置序列版本号,系统将默认提供一个。若对属性进行修改等操作,导致默认提供的序列版本号不一致。
public class SerializableTest {
@Test
public void test1() throws FileNotFoundException, IOException {
//使用默认的序列版本号
Student s1=new Student(1001,"xiaoming");
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("hello.txt"));
oos.writeObject(s1);
oos.close();
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("hello.txt"));
try {
Student s=(Student)ois.readObject();
ois.close();
System.out.println(s);
} catch (ClassNotFoundException | IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
class Student implements Serializable{
private int id;
private String name;
public Student() {
super();
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
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;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
public class SerializableTest {
@Test
public void test1() throws FileNotFoundException, IOException {
//此时系统提供一个默认的序列版本号
Student s1=new Student(1001,"xiaoming");
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("hello.txt"));
oos.writeObject(s1);
oos.close();
// ObjectInputStream ois=new ObjectInputStream(new FileInputStream("hello.txt"));
// try {
// Student s=(Student)ois.readObject();
// ois.close();
// System.out.println(s);
// } catch (ClassNotFoundException | IOException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }
}
}
class Student implements Serializable{
private int id;
private String name;
// private String major;
public Student() {
super();
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
// public Student(int id, String name, String major) {
// super();
// this.id = id;
// this.name = name;
// this.major = major;
// }
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;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
// public String getMajor() {
// return major;
// }
//
// public void setMajor(String major) {
// this.major = major;
// }
//
// @Override
// public String toString() {
// return "Student [id=" + id + ", name=" + name + ", major=" + major + "]";
// }
}
public class SerializableTest {
@Test
public void test1() throws FileNotFoundException, IOException {
//此时我去修改Student类,添加了一个属性
// Student s1=new Student(1001,"xiaoming");
// ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("hello.txt"));
// oos.writeObject(s1);
// oos.close();
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("hello.txt"));
try {
Student s=(Student)ois.readObject();
ois.close();
System.out.println(s);
} catch (ClassNotFoundException | IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
class Student implements Serializable{
private int id;
private String name;
private String major;
public Student() {
super();
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Student(int id, String name, String major) {
super();
this.id = id;
this.name = name;
this.major = major;
}
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 getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", major=" + major + "]";
}
}
从结果可以看出:前后两次的序列版本号不一致,所以我们应该设置序列版本号慎用系统默认提供的。
public class SerializableTest {
@Test
public void test1() throws FileNotFoundException, IOException {
Student s1=new Student(1001,"xiaoming","software","qinghua");
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("hello.txt"));
oos.writeObject(s1);
oos.close();
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("hello.txt"));
try {
Student s=(Student)ois.readObject();
ois.close();
System.out.println(s);
} catch (ClassNotFoundException | IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
class Student implements Serializable{
private static final long serialVersionUID = -6849794456456710L;
private int id;
private String name;
private String major;
private static String nation="China";
private transient String shcool="lanjiao";
public Student() {
super();
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Student(int id, String name, String major) {
super();
this.id = id;
this.name = name;
this.major = major;
}
public Student(int id, String name, String major, String shcool) {
super();
this.id = id;
this.name = name;
this.major = major;
this.shcool = shcool;
}
public static String getNation() {
return nation;
}
public static void setNation(String nation) {
Student.nation = nation;
}
public String getShcool() {
return shcool;
}
public void setShcool(String shcool) {
this.shcool = shcool;
}
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 getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", major=" + major + ", shcool=" + shcool + "]";
}
}
staitc、transient修饰的属性不参与序列化,反序列化则按最新值处理
关于RandomAccessFile类的使用
mode: ①r:只读 ②rw:读写
默认从头开始覆盖文件
public class RandomAccessInputStreamTest {
@Test
public void test1() throws IOException {
File file=new File("hello.txt");
RandomAccessFile ris=new RandomAccessFile(file,"rw");
ris.write("aaaaaaaa".getBytes());
ris.close();
}
}
seek(int index):默认从头开始,对内容进行覆盖
@Test
public void test1() throws IOException {
// abcdefghijk -> abmmmfghijk
RandomAccessFile raf=new RandomAccessFile("hello.txt","rw");
raf.seek(2);
raf.write("mmm".getBytes());
raf.close();
}
@Test
public void test2() throws IOException {
//使用seek()方法实现一个插入功能
// abcd -> abmmcd
RandomAccessFile raf=new RandomAccessFile("hello.txt","rw");
raf.seek(2);
byte [] buffer=new byte[(int)new File("hello.txt").length()];
raf.read(buffer);
raf.seek(2);
raf.write("mm".getBytes());
raf.write(buffer);
raf.close();
}
TCP网络编程
/*
* InetAddress实例化的两个方法:①getByName②getLocalHost
*/
@Test
public void test1() throws UnknownHostException {
InetAddress address=InetAddress.getByName("127.0.0.1");
InetAddress address1=InetAddress.getLocalHost();
System.out.println(address);
System.out.println(address1);
}
//客户端
@Test
public void Client() throws IOException {
InetAddress address =InetAddress.getByName("127.0.0.1");
Socket socket=new Socket(address,9090);
OutputStream os=socket.getOutputStream();
os.write("你好美女!可以加个微信吗?".getBytes());
os.close();
socket.close();
}
//服务端
@Test
public void Server() throws IOException {
ServerSocket ss=new ServerSocket(9090);
Socket socket=ss.accept();
InputStream is=socket.getInputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int len;
byte [] b=new byte [10];
while((len=is.read(b))!=-1) {
baos.write(b, 0, len);
}
System.out.println(baos.toString());
baos.close();
is.close();
socket.close();
ss.close();
@Test
public void Client() throws IOException {
Socket socket =new Socket(InetAddress.getLocalHost(),9090);
OutputStream os=socket.getOutputStream();
BufferedReader br=new BufferedReader(new FileReader("hello.txt"));
String str;
while((str=br.readLine())!=null) {
os.write(str.getBytes());
}
System.out.println("发送成功!");
socket.shutdownOutput();
InputStream is=socket.getInputStream();
ByteArrayOutputStream baf=new ByteArrayOutputStream();
int len;
byte [] b=new byte [10];
while((len=is.read(b))!=-1) {
baf.write(b, 0, len);
}
System.out.println(baf.toString());
baf.close();
is.close();
os.close();
br.close();
socket.close();
}
@Test
public void Server() throws IOException, IOException {
ServerSocket ss=new ServerSocket(9090);
Socket socket=ss.accept();
InputStream is=socket.getInputStream();
OutputStream os=socket.getOutputStream();
int len;
byte [] b=new byte[1024];
while((len=is.read(b))!=-1) {
String s=new String(b);
String str=s.toUpperCase();
System.out.println(str);
os.write(str.getBytes());
}
os.close();
is.close();
socket.close();
ss.close();
}
UDP编程
@Test
public void sender() throws SocketException, IOException {
DatagramSocket socket=new DatagramSocket();
String str="曾经意外,他和她相爱,在没有犹豫的年代";
byte [] b=str.getBytes();
DatagramPacket packet =new DatagramPacket(b,0,b.length,InetAddress.getLocalHost(),9090);
socket.send(packet);
socket.close();
}
@Test
public void receiver() throws IOException {
DatagramSocket socket=new DatagramSocket(9090);
byte [] b=new byte [1024];
DatagramPacket packet=new DatagramPacket(b,0,b.length);
socket.receive(packet);
System.out.println(new String(packet.getData()));
socket.close();
}
URL编程
@Test
public void urlTest() throws IOException {
URL url=new URL("http://localhost:8080/fuqibin/2932.png");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.connect();
InputStream is=connection.getInputStream();
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("932.png"));
int len;
byte [] buffer =new byte [1024];
while((len=is.read(buffer))!=-1) {
bos.write(buffer, 0, len);
}
is.close();
bos.close();
connection.disconnect();
}