• 69期-Java SE-022-IO流-3-001-002


    ### 缓冲输出流
    
    ```java
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    public class Test {
        public static void main(String[] args) {
            File file = new File("/Users/southwind/Desktop/test.txt");
            OutputStream outputStream;
            try {
                outputStream = new FileOutputStream(file);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                String str = "Java是一门面向对象的编程语言";
                byte[] bytes = str.getBytes();
                bufferedOutputStream.write(97);
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    ```
    
    
    
    ### 序列化和反序列化
    
    序列化就是将内存中的对象输出到硬盘文件中进行保存,反序列化就是将硬盘中保存的数据还原成内存中的对象。
    
    - 序列化
    
    ```java
    import java.io.Serializable;
    
    public class User implements Serializable {
        private int id;
        private String name;
        private int age;
        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;
        }
        
    }
    ```
    
    
    
    ```java
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    
    public class Test2 {
        public static void main(String[] args) {
            User user = new User();
            user.setId(1);
            user.setName("张三");
            user.setAge(22);
            File file = new File("/Users/southwind/Desktop/java.txt");
            try {
                OutputStream outputStream = new FileOutputStream(file);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                objectOutputStream.writeObject(user);
                objectOutputStream.flush();
                objectOutputStream.close();
                outputStream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    ```
    
    
    
    - 反序列化
    
    ```java
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    
    public class Test3 {
        public static void main(String[] args) {
            File file = new File("/Users/southwind/Desktop/java.txt");
            try {
                InputStream inputStream = new FileInputStream(file);
                ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
                User user = (User) objectInputStream.readObject();
                System.out.println(user.getId());
                System.out.println(user.getName());
                System.out.println(user.getAge());
                objectInputStream.close();
                inputStream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    ```
    
    
    
    ```java
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.Reader;
    import java.io.Writer;
    
    public class Test4 {
        public static void main(String[] args) {
            //使用IO流完成文件复制
            File file = new File("/Users/southwind/Desktop/1.jpg");
            try {
    //            InputStream inputStream = new FileInputStream(file);
    //            byte[] bytes = new byte[100];
    //            int length = inputStream.read(bytes);
    //            File file2 = new File("/Users/southwind/Desktop/test2.txt");
    //            OutputStream outputStream = new FileOutputStream(file2);
    //            outputStream.write(bytes,0,length);
                
                InputStream inputStream = new FileInputStream(file);
                File file2 = new File("/Users/southwind/Desktop/2.jpg");
                OutputStream outputStream = new FileOutputStream(file2);
                int temp = 0;
                while((temp = inputStream.read())!=-1) {
                    outputStream.write(temp);
                }
                
    //            Reader reader = new FileReader(file);
    //            int temp = 0;
    //            Writer writer = new FileWriter(file2);
    //            while((temp = reader.read())!=-1) {
    //                writer.write(temp);
    //            }
    //            writer.close();
    //            reader.close();
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    ```
    
    
    
    ```java
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Reader;
    import java.io.Writer;
    
    public class Test5 {
        public static void main(String[] args) {
            File file = new File("/Users/southwind/Desktop/1.jpg");
            try {
    //            InputStream inputStream = new FileInputStream(file);
    //            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    //            File file2 = new File("/Users/southwind/Desktop/2.jpg");
    //            OutputStream outputStream = new FileOutputStream(file2);
    //            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
    //            int temp = 0;
    //            while((temp = bufferedInputStream.read())!=-1) {
    //                bufferedOutputStream.write(temp);
    //            }
    //            bufferedInputStream.close();
    //            bufferedOutputStream.close();
    //            inputStream.close();
    //            outputStream.close();
                
                InputStream inputStream = new FileInputStream(file);
                Reader reader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(reader);
                File file2 = new File("/Users/southwind/Desktop/2.jpg");
                OutputStream outputStream = new FileOutputStream(file2);
                Writer writer = new OutputStreamWriter(outputStream);
                BufferedWriter bufferedWriter = new BufferedWriter(writer);
                int temp = 0;
                while((temp = bufferedReader.read())!=-1) {
                    bufferedWriter.write(temp);
                }
                bufferedWriter.close();
                bufferedReader.close();
                reader.close();
                writer.close();
                inputStream.close();
                outputStream.close();
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    ```
    
    
    
    ### 反射
    
    常规情况下是通过类来创建实例化对象的,反射就是将这一过程进行反转,通过实例化对象来获取对应的类的信息。
    
    ### Class
    
    Class 类是反射的源头,Class 类是专门用来描述其他类的类,Class 的每一个实例化对象就对应一个类的结构。
    
    - 调用 Class 类的静态方法 forName(String className),将目标类的全限定类名(包含所在包信息的类名全程)作为参数传入,即可获取对应的 Class 对象。
    
      ```java
      try {
        Class clazz = Class.forName("com.test.entity.User");
        System.out.println(clazz);
      } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      ```
    
    - 通过目标类的 class 创建,Java 中的每一个类都可以调用 class,这里的 class 不是属性,叫做"类字面量",其作用是获取内存中目标类型 class 对象的引用。
    
      ```java
      Class clazz2 = User.class;
      System.out.println(clazz2);
      ```
    
    - 通过目标类实例化对象的 getClass() 方法创建,getClass() 方法定义在 Object 类中,被所有的类继承,通过实例化对象获取内存中改类 class 对象的引用。
    
      ```java
      User user = new User();
      Class clazz3 = user.getClass();
      System.out.println(clazz3);
      ```
    
    Class 的常用方法
    
    public native boolean isInterface()                                                                判断目标类是否为接口
    
    public native boolean isArray()                                                                        判断目标类是否为数组
    
    public native boolean isAnnotation()                                                            判断目标类是否为注解
    
    public String getName()                                                                                    获取目标类的全限定类名
    
    public native Class getSuperclass()                                                                获取目标类的父类

    Test.java

    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    public class Test {
        public static void main(String[] args) {
            File file = new File("/Users/southwind/Desktop/test.txt");
            OutputStream outputStream;
            try {
                outputStream = new FileOutputStream(file);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                String str = "Java是一门面向对象的编程语言";
                byte[] bytes = str.getBytes();
                bufferedOutputStream.write(97);
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    Test.2java

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    
    public class Test2 {
        public static void main(String[] args) {
            User user = new User();
            user.setId(1);
            user.setName("张三");
            user.setAge(22);
            File file = new File("/Users/southwind/Desktop/java.txt");
            try {
                OutputStream outputStream = new FileOutputStream(file);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                objectOutputStream.writeObject(user);
                objectOutputStream.flush();
                objectOutputStream.close();
                outputStream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    Test3.java

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    
    public class Test3 {
        public static void main(String[] args) {
            File file = new File("/Users/southwind/Desktop/java.txt");
            try {
                InputStream inputStream = new FileInputStream(file);
                ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
                User user = (User) objectInputStream.readObject();
                System.out.println(user.getId());
                System.out.println(user.getName());
                System.out.println(user.getAge());
                objectInputStream.close();
                inputStream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    Test4.java

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.Reader;
    import java.io.Writer;
    
    public class Test4 {
        public static void main(String[] args) {
            //使用IO流完成文件复制
            File file = new File("/Users/southwind/Desktop/1.jpg");
            try {
    //            InputStream inputStream = new FileInputStream(file);
    //            byte[] bytes = new byte[100];
    //            int length = inputStream.read(bytes);
    //            File file2 = new File("/Users/southwind/Desktop/test2.txt");
    //            OutputStream outputStream = new FileOutputStream(file2);
    //            outputStream.write(bytes,0,length);
                
                InputStream inputStream = new FileInputStream(file);
                File file2 = new File("/Users/southwind/Desktop/2.jpg");
                OutputStream outputStream = new FileOutputStream(file2);
                int temp = 0;
                while((temp = inputStream.read())!=-1) {
                    outputStream.write(temp);
                }
                
    //            Reader reader = new FileReader(file);
    //            int temp = 0;
    //            Writer writer = new FileWriter(file2);
    //            while((temp = reader.read())!=-1) {
    //                writer.write(temp);
    //            }
    //            writer.close();
    //            reader.close();
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    Test5.java

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Reader;
    import java.io.Writer;
    
    public class Test5 {
        public static void main(String[] args) {
            File file = new File("/Users/southwind/Desktop/test.txt");
            try {
                InputStream inputStream = new FileInputStream(file);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                File file2 = new File("/Users/southwind/Desktop/test2.txt");
                OutputStream outputStream = new FileOutputStream(file2);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                int temp = 0;
                while((temp = bufferedInputStream.read())!=-1) {
                    bufferedOutputStream.write(temp);
                }
                bufferedInputStream.close();
                bufferedOutputStream.close();
                inputStream.close();
                outputStream.close();
                
    //            InputStream inputStream = new FileInputStream(file);
    //            Reader reader = new InputStreamReader(inputStream);
    //            BufferedReader bufferedReader = new BufferedReader(reader);
    //            File file2 = new File("/Users/southwind/Desktop/test2.txt");
    //            OutputStream outputStream = new FileOutputStream(file2);
    //            Writer writer = new OutputStreamWriter(outputStream);
    //            BufferedWriter bufferedWriter = new BufferedWriter(writer);
    //            String str = "";
    //            while((str = bufferedReader.readLine())!=null) {
    //                bufferedWriter.write(str);
    //            }
    //            bufferedWriter.close();
    //            bufferedReader.close();
    //            reader.close();
    //            writer.close();
    //            inputStream.close();
    //            outputStream.close();
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    Test6.java

    public class Test6 {
        public static void main(String[] args) {
            
        }
    }

    User.java

    import java.io.Serializable;
    
    public class User implements Serializable {
        private int id;
        private String name;
        private int age;
        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;
        }
        
    }

    Test.java

    package com.test.entity;
    
    public class Test {
        public static void main(String[] args) {
            try {
                Class clazz = Class.forName("com.test.entity.User");
    //            System.out.println(clazz);
    //            Class clazz2 = User.class;
    //            System.out.println(clazz2);
    //            User user = new User();
    //            Class clazz3 = user.getClass();
    //            System.out.println(clazz3);
    //            System.out.println(clazz.hashCode());
    //            System.out.println(clazz2.hashCode());
    //            System.out.println(clazz3.hashCode());
    //            System.out.println(clazz == clazz2);
    //            System.out.println(clazz2 == clazz3);
                
                System.out.println(clazz.isInterface());
                System.out.println(clazz.isArray());
                System.out.println(clazz.isAnnotation());
                System.out.println(clazz.getName());
                System.out.println(clazz.getSuperclass());
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    User.java

    package com.test.entity;
    
    public class User {
        
    }
  • 相关阅读:
    EJB到底是什么,真的那么神秘吗??
    An Assembly Language
    Memory Layout (Virtual address space of a C process)
    手机测试用例-游戏测试用例
    手机测试用例-工具箱测试用例
    手机测试用例-输入法测试用例
    手机测试用例-时钟测试用例
    手机测试用例-多媒体测试用例
    手机测试用例-设置测试用例
    手机测试用例-通话记录测试用例
  • 原文地址:https://www.cnblogs.com/HiJackykun/p/11173232.html
Copyright © 2020-2023  润新知