Java学习_Day 19(学习内容:尚硅谷IO流与网络编程JAVA零基础P613-P624)

P613 IO流与网络编程-自定义类实现序列化与反序列化操作

P614 IO流与网络编程-serivalVersionUID的理解

P615 IO流与网络编程-自定义可序列化的其他要求

// 自定义类下的属性也应该是可序列化的
// 自定义类序列化和反序列化无法识别static和transient修饰的成员变量

P616 IO流与网络编程-RandomAccessFile实现文件的读写操作

package com.io;

import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileTest {
    /*
    既可以作为一个输入流,又可作为一个输出流
     */
    @Test
    public void test1() throws FileNotFoundException {
        RandomAccessFile raf1 = null;
        RandomAccessFile raf2 = null;
        try {
            raf1 = new RandomAccessFile(new File("xx.jpg"), "r");
            raf2 = new RandomAccessFile(new File("xxx.jpg"), "rw");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = raf1.read(buffer)) != -1){
                raf2.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (raf1 != null){
                try {
                    raf1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (raf2 != null){
                try {
                    raf2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

P617 IO流与网络编程-RandomAccessFile实现数据的插入

    @Test
    public void test2() throws IOException{
        RandomAccessFile raf1 = new RandomAccessFile("hello.txt", "rw");
        raf1.seek(3);
        raf1.write("xyz".getBytes());
        raf1.close();
    }

    /*
    实现数据插入效果
     */
    @Test
    public void test3() throws IOException {
        RandomAccessFile raf1 = new RandomAccessFile("hello.txt", "rw");
        StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());
        raf1.seek(3);
        byte[] buffer = new byte[20];
        int len;
        while ((len = raf1.read(buffer)) != -1){
            builder.append(new String(buffer, 0, len));
        }
        raf1.seek(3);
        raf1.write("xyz".getBytes());

        raf1.write(builder.toString().getBytes());
        raf1.close();
    }

P618 IO流与网络编程-NIO介绍及NIO2中Path、Paths和File的介绍

P619 IO流与网络编程-使用第三方Jar包实现数据读写

P620 IO流与网络编程-网络编程概述

IP和端口号、网络通信协议

P621 IO流与网络编程-IP的理解与InetAddress类的实例化

package com.net;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressTest {
    public static void main(String[] args) {
        try {
            InetAddress inet1 = InetAddress.getByName("192.168.10.14");
            System.out.println(inet1);
            InetAddress inet2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inet2);
            InetAddress inet3 = InetAddress.getByName("127.0.0.1");
            System.out.println(inet3);

            InetAddress inet4 = InetAddress.getLocalHost();
            System.out.println(inet4);

            System.out.println(inet2.getHostName());
            System.out.println(inet2.getHostAddress());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }
}

P622 IO流与网络编程-端口号的理解

socket = IP + 端口号

P623 IO流与网络编程-TCP和UDP网络通信协议的对比

TCP:三次握手建立连接。需要释放已建立的连接,效率低。类似于打电话。释放连接需要四次挥手。
UDP:不需要建立连接,只需封装成数据包。不可靠的,开销小,速度快。类似于发短信。

P624 IO流与网络编程-TCP网络编程例题1

package com.net;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPTest1 {
    /*
    TCP网络编程例子1
     */
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            // 1. 创建socket对象,指明服务器端的IP和端口号
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet, 8899);
            // 2. 获取一个输出流,用于输出数据
            os = socket.getOutputStream();
            // 3. 写出数据的操作
            os.write("你好,我是客户端".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            // 1.创建服务器端的ServerSocket
            ss = new ServerSocket(8899);
            // 2.调用accept表示接收来自于客户端的socket
            socket = ss.accept();
            // 3.造流
            is = socket.getInputStream();
//  不建议这么写
//        byte[] buffer = new byte[20];
//        int len;
//        while ((len = is.read(buffer)) != -1){
//            String str = new String(buffer, 0, len);
//            System.out.print(str);
//        }
            // 4.读取输入流的数据
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[5];
            int len;
            while ((len = is.read(buffer)) != -1){
                baos.write(buffer, 0, len);
            }
            System.out.println(baos.toString());
            System.out.println(socket.getInetAddress().getHostAddress());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (baos != null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

你可能感兴趣的:(JAVA学习,java,网络,学习)