• Java高级编程day25【谷】


    Java高级编程day25

    对象序列化机制

    允许把内存中的Java对象转换成与平台无关的二进制流,从而允许把这种二进制流持久的保存在磁盘上,或通过网络将这种二进制传输到另一个网络节点,

    当其他程序获取这种二进制流,就可以恢复为原来的Java对象

    对象流

    序列化:将内存中的Java对象转换成与平台无关的二进制流

    public class test {
        public static void main(String[] args) {
            ObjectOutputStream oos = null;
            try {
                FileOutputStream fileOutputStream = new FileOutputStream("dest.dat");
                oos = new ObjectOutputStream(fileOutputStream);
                oos.writeObject("我爱中国,我爱共产党");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    反序列化:将磁盘文件中的对象还原为内存中的一个java对象

    public class TestJie {
        public static void main(String[] args) {
            ObjectInputStream ois = null;
            try {
                FileInputStream fileInputStream = new FileInputStream("dest.dat");
                ois = new ObjectInputStream(fileInputStream);
                Object o = ois.readObject();
                String str = (String) o;
                System.out.println(str);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (ois != null) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    如果对象流要传的数据为自定义类

    我们要将自定义类型序列化

    1.需要实现接口:Serializable

    2.当前类提供一个全局变量:serialVersionUID

    执行如上两部即可序列化

    3.除了当前的类需要实例化外,必须保证内部的所有属性都必须是可实例化的(默认情况下,基本数据类型都是可实例化的)

    补充:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量

    RandomAccessFile

    1.RandomAccessFile直接继承java.lang.Object类,实现了DataInput和DataOutput接口

    2.RandomAccessFile即可作为输入流也可以作为输出流,却决于构造器是 r还是rw

    3.如果RandomAccessFiLe作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建
    如果写出到的文件存在,则会对原有文件内容自行覆盖。(默认情况下,从头覆盖)

    seek方法:将指针调到角标为几的位置,write方法将从指定角标位置进行覆盖

    RandomAccessFile类应用:多线程下载

    main方法中文件的相对路径为Poject类

    Junit测试在文件的相对路径在当前的Module下

    我们一般在开发时会使用第三方jar包来简化开发

    NIO是IO的进阶产品

    NIO2因为NIO不太好用,NIO2出现

    NIO2中Path相当于IO中的File且二者可以相互转换

    网络编程

    目的

    直接或间接地通过网络协议与其他计算机实现数据交互,进行通信

    两个主要的问题

    如何准确地定位网络上一台主机或多台主机;定位主机上的特定应用

    找到主机后如何高效进行数据传输

    网络通信要素

    IP和端口号

    IP地址:InetAddress

    在Java中使用InetAddress类表示IP

    唯一的标识Internet上的计算机(通信实体)

    本地回环地址:127.0.0.1 主机名(hostName):localhost

    IP分类:IPV4与IPV6;万维网和局域网

    域名:www.baidu.com www.mi.com

    实例化InetAddress的两个方法:getByName(String host)

    getLocalHost():获取本地IP地址

    常用的两个方法getHostName() getHostAddress()

    端口号

    正在计算机上运行的进程

    要求:不同的进程由不同的端口号

    范围:被规定为16位的整数0~65535

    代表一台主机上不同的应用程序

    网络通信协议

    TCP/IP参考模型(应用层、传输层、网络层、物理+数据链路层)

    端口号与IP地址的组合得出一个网络套接字:Socket

    TCP网络编程

    客户端与服务端通信

    客户端

    1.创建Socket对象,指明服务器端的ip的端口号

    2.获取一个输出流,用于输出数据

    3.写入数据的操作

    4.资源的关闭

    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            InetAddress inetAddress = InetAddress.getByName("192.168.100.101");
            socket = new Socket(inetAddress, 8888);
            os = socket.getOutputStream();
            os.write("我爱你中国,我爱你刘晶晶".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    服务端

    1.创建服务器端的ServerSocket,指明自己的窗口号

    2.调用accept()表示接收来自于客户端的Socket

    3.获取输入流

    4.读取输入流中的数据

    //服务端
    @Test
    public void service() {
        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            serverSocket = new ServerSocket(8888);
            accept = serverSocket.accept();
            is = accept.getInputStream();
            baos = new ByteArrayOutputStream();
            byte[] bytes = new byte[5];
            int len;
            while ((len = is.read(bytes)) != -1) {
                baos.write(bytes, 0, len);
            }
            System.out.println(baos.toString());
            System.out.println("收到了来自于:" + serverSocket.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 (accept != null) {
    
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if ( serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    升级版:客户端向服务器端发送文件,服务器端将文件保存在本地,之后给客户端反馈,客户端将反馈信息打印到控制台

    客户端

    //客户端
    @Test
    public void client() {
        OutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        Socket socket = null;
        ByteArrayOutputStream stream=null;
        try {
            socket = new Socket("127.0.0.1", 8999);
            outputStream = socket.getOutputStream();
            fileInputStream = new FileInputStream("1.jpg");
            byte[] bytes = new byte[10];
            int len;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            socket.shutdownOutput();
            InputStream ips = socket.getInputStream();
            stream = new ByteArrayOutputStream();
            byte[] bytess = new byte[10];
            int lens;
            while ((lens= ips.read(bytess))!=-1){
                stream.write(bytess,0,lens);
            }
            System.out.println(stream.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
    
                try {
                    fileInputStream.read();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(stream!=null){
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    服务端

    //服务端
    @Test
    public void service() {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        OutputStream outputStream=null;
        try {
            serverSocket = new ServerSocket(8999);
            socket = serverSocket.accept();
            is = socket.getInputStream();
            fos = new FileOutputStream("3.png");
            byte[] bytes = new byte[10];
            int len;
            while ((len = is.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
            outputStream = socket.getOutputStream();
            outputStream.write("收到你的数据了!!!".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.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 (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
            if(outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    UDP网络编程

    1.实例化DatagramSocket类

    2.实例化DatagramPacket,使用构造器指定数据以及地址

    3.调用实例化DatagramSocket类的方法send/recive

    4.关闭资源

    发送端
    @Test
    //发送端
    public void client() {
        DatagramSocket datagramSocket = null;
        try {
            datagramSocket = new DatagramSocket();
            String s = "我用UDP导弹干你";
            byte[] bytes = s.getBytes();
            InetAddress localHost = InetAddress.getLocalHost();
            DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, localHost, 8899);
            datagramSocket.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (datagramSocket != null) {
                datagramSocket.close();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    接收端

    @Test
    //接收端
    public void service() {
        DatagramSocket datagramSocket = null;
        try {
            datagramSocket = new DatagramSocket(8899);
            byte[] bytes = new byte[100];
            DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
            datagramSocket.receive(packet);
            byte[] data = packet.getData();
            String str = new String(data);
            System.out.println(str);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            datagramSocket.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    URL编程

    1.URL:统一资源定位符,对应着互联网的某一资源地址

    2.格式

    http://localhost:8080/examples/beauty.jpg?username=Tom

    协议 主机名 端口号 资源地址 参数列表

    public String getProtocol()获取该URL的协议名
    public String getHost()获取该URL的主机名
    public String getport()获取该URL的端口号
    public String getPath()获取该URL的文件路径
    public String getFile()获取该URL的文件名
    public String getQuery()获取该URL的查询名

    网络资源下载到本地文件

    注意:openConnection()方法要声明为HttpURLConnection

    要关闭连接!!!!!!!!!

    public class URLPhotoTest {
        public static void main(String[] args) {
            HttpURLConnection urlConnection = null;
            InputStream inputStream = null;
            FileOutputStream fileOutputStream = null;
            try {
                URL url = new URL("https://img2.baidu.com/it/u=2858376042,4067908290&fm=253&fmt=auto&app=120&f=JPEG?w=640&h=1280");
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.connect();
                inputStream = urlConnection.getInputStream();
                fileOutputStream = new FileOutputStream("beauty.png");
                byte[] bytes = new byte[1024];
                int len;
                while ((len = inputStream.read(bytes)) != -1) {
                    fileOutputStream.write(bytes, 0, len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                }
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                }
                urlConnection.disconnect();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    改变自己 只需要两年
    docker到底能在哪些平台安装?
    如何为java项目开启Spring Boot模式呢?
    QT creator实现串口通信操作方法
    frp内网穿透ssh,tcp经过服务器慢速和p2p模式实现高速吃满上传带宽
    初识 Nginx - 概念篇 - 细节狂魔
    第十五章 Spring Cloud Alibaba 入门介绍
    北斗导航 | GNSS数据处理:预处理与参数估计模型
    计算机毕业设计Java基本web蓝桥杯名师工作室(源码+系统+mysql数据库+lw文档)
    软考高级之系统架构师之数据流图和流程图
  • 原文地址:https://blog.csdn.net/m0_47711130/article/details/126175024