1.序列化概念
Java自带序列化接口
|
1
2
3
4
5
6
7
|
private static void encoding() throws Exception { ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("E:/test/obj1.txt")); SerializableUser user = new SerializableUser(); user.set("9521", "周星星", 23, 10000D); os.writeObject(user); os.close();} |
|
1
2
3
4
5
6
7
|
private static void decoding() throws Exception { ObjectInputStream oi = new ObjectInputStream(new FileInputStream("E:/test/obj1.txt")); Object object = oi.readObject(); SerializableUser user = (SerializableUser) object; System.out.println(user); oi.close();} |
自定义序列化(FileOutputStream)
将对象转成 json 格式再序列化
|
01
02
03
04
05
06
07
08
09
10
11
|
private static void encoding2() throws Exception { SerializableUser user = new SerializableUser(); user.set("9521", "周星星", 23, 10000D); Gson gson = new Gson(); String json = gson.toJson(user); System.out.println(json); FileOutputStream fos = new FileOutputStream("E:/test/obj2.txt"); fos.write(json.getBytes("utf-8")); fos.flush(); fos.close();} |
|
1
2
3
4
5
6
7
8
|
private static void decoding2() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader((new FileInputStream("E:/test/obj2.txt")))); String json = br.readLine(); br.close(); Gson gson = new Gson(); SerializableUser user = gson.fromJson(json, SerializableUser.class); System.out.println(user);} |
通过 DataOutputStream 优化上述代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
|
private static void encoding3() throws Exception { SerializableUser user = new SerializableUser(); user.set("9521", "周星星", 23, 10000D); DataOutputStream dos = new DataOutputStream(new FileOutputStream("E:/test/obj3.txt")); dos.writeUTF(user.getId()); dos.writeUTF(user.getName()); dos.writeInt(user.getAge()); dos.writeDouble(user.getSalary()); dos.flush(); dos.close();} |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
|
private static void decoding3() throws Exception { SerializableUser user = new SerializableUser(); DataInputStream dis = new DataInputStream(new FileInputStream("E:/test/obj3.txt")); String id = dis.readUTF(); String name = dis.readUTF(); int age = dis.readInt(); double salary = dis.readDouble(); user.set(id, name, age, salary); System.out.println(user); dis.close();} |
总结
2 服务概念
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
public class SockerServer1 { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务端启动..."); while (true) { Socket socket = serverSocket.accept(); System.out.println("服务端收到消息:"); InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } }} |
|
01
02
03
04
05
06
07
08
09
10
|
public class SocketClient1 { public static void main(String[] args) throws IOException { Socket socket = new Socket("localhost",8888); OutputStream os = socket.getOutputStream(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); bw.write("Hello Dog!"); bw.flush(); bw.close(); }} |
可以通过浏览器访问,打印出HTTP协议
|
01
02
03
04
05
06
07
08
09
10
11
|
服务端启动...服务端收到消息:GET / HTTP/1.1Host: localhost:8888Connection: keep-aliveCache-Control: max-age=0Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8Accept-Encoding: gzip, deflate, brAccept-Language: zh-CN,zh;q=0.9,en;q=0.8 |
web服务(restApi)
3 进程与线程
4 文件格式概念
行式存储
列式存储
5 迭代器
原文:https://www.cnblogs.com/heimaguangzhou/p/11399035.html