• JavaIO流


    也传到了码云上,还是码云上看着舒服

    按照处理的字节数

    • 字节流

    • 字符流

    按照方向分类

    • 输入流

    • 输出流

    按照功能

    • 节点流

    • 处理流

    相关代码

        /**
         *     节点流-输出流
         */
        public static void output() {
            FileOutputStream fos = null;
            File file = new File("demo0609.txt");
            String str = "mark";
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            try {
                fos = new FileOutputStream(file);
                fos.write(str.getBytes());
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        /**
         *     节点流-输入流
         */
        public static void input() {
            FileInputStream fis = null;
            File file = new File("demo0609.txt");
            int result = 0;
            try {
                fis = new FileInputStream(file);
                while((result=fis.read())!=-1) {
                    System.out.println((char)result);
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        /**
         *     处理流-输出流
         */
        public static void outputBuffered() {
            FileOutputStream fos = null;
            OutputStreamWriter osw = null;
            BufferedWriter bw = null;
            File file = new File("test.txt");
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            try {
                fos = new FileOutputStream(file);
                osw = new OutputStreamWriter(fos);
                bw = new BufferedWriter(osw);
                String[] a = {"sfds", "eeee"};
                for (int i = 0; i < a.length; i++) {
                    bw.write(a[i]);
                    bw.newLine();
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    bw.close();
                    osw.close();
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        /**
         *     处理流-输入流
         */
        public static void inputBuffered() {
            FileInputStream fis = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            File  file = new File("test.txt");
            String result = "";
            try {
                fis = new FileInputStream(file);
                isr = new InputStreamReader(fis);
                br = new BufferedReader(isr);
                while((result=br.readLine())!=null) {
                    System.out.println(result);
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void serializable() {
            OutputStream os = null;
            ObjectOutputStream oos = null;
    
        }
    

    序列化和反序列化

    类的对象会随着程序的终止而被垃圾收集器销毁。如果要在不重新创建对象的情况下调用该类,该怎么做?这就可以通过序列化将数据转换为字节流。 对象序列化是一个用于将对象状态转换为字节流的过程,可以将其保存到磁盘文件中或通过网络发送到任何其他程序;从字节流创建对象的相反的过程称为反序列化。而创建的字节流是与平台无关的,在一个平台上序列化的对象可以在不同的平台上反序列化。 需要实现序列化的对象要实现Serializable接口

        /**
         * 序列化
         */
        public static void serializable() {
            Student stu = new Student("123", "mark", 22);
            FileOutputStream fos = null;
            ObjectOutputStream oos = null;
            File file = new File("demo02.txt");
            try {
                fos = new FileOutputStream(file);
                oos = new ObjectOutputStream(fos);
                oos.writeObject(stu);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    oos.close();
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            }
        }
        /**
         * 反序列化
         */
        public static void unserializable() {
            FileInputStream fis = null;
            ObjectInputStream ois = null;
            File file = new File("demo02.txt");
            if(!file.exists()) {
                System.out.println("文件不存在");
                return;
            }
            try {
                fis = new FileInputStream(file);
                ois = new ObjectInputStream(fis);
                Object o = ois.readObject();
                if(o instanceof Student) {
                    Student stu = (Student)o;
                    System.out.println(stu.toString());
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    ois.close();
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            }
        }
    

    MarkText 编写
    mark

  • 相关阅读:
    C++ unordered_set运用实例
    C++ Multimap运用实例—查找元素
    C++ Multimap运用实例
    C++ Map运用实例
    C++ Set运用实例
    C++ list运用实例
    C++ vector使用实例
    c++ Array运用实例
    C++ int double float对应的长度以及二进制
    引用和指针有什么区别
  • 原文地址:https://www.cnblogs.com/MC-Curry/p/11005516.html
Copyright © 2020-2023  润新知