zoukankan      html  css  js  c++  java
  • Java文件与io——打印流和对象流

    打印流的主要功能是用于输出,在整个IO包中打印流分为两种类型:

    字节打印流:PrintStream

    字符打印流:PrintWriter

    打印流可以很方便的进行输出

    public class PrintStreamDemo {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            byteStreamOut();
            charStreamOut();
        }
        //PrintStream
        public static void byteStreamOut(){
            try {
                OutputStream out=new FileOutputStream("G:\hhb.txt");
                BufferedOutputStream bos=new BufferedOutputStream(out);
                PrintStream ps=new PrintStream(bos);//更加方便的输出
                //bos.write("hhhhh".getBytes());
                ps.println(123);
                ps.close();
                bos.close();
                out.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        //PrintWriter
        public static void charStreamOut(){
            try {
                Writer w=new FileWriter("G:\hhh.txt");
                BufferedWriter bw=new BufferedWriter(w);
                PrintWriter pw=new PrintWriter(bw);
                pw.println(343);
                pw.println("lll");
                //bw.write("我爱你!");
                pw.close();
                bw.close();
                w.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }

    对象流

    对象序列化

    对象序列化就是指将一个对象转化为二进制的byte流。

    两种情况:

    将对象保存在文件上的操作称为对象的序列化操作

    将对象从文件之中恢复称为反序列化的操作

    使用ObjectOutputStream类实现对象的序列化,使用ObjectInputStream类实现反序列。

    被序列化的对象所在的类必须实现java.io.Serializable接口

    public class Cat implements Serializable{
        private String name;
        private int age;
        private transient String color;//关键字transient,表示不用序列化
        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;
        }
        public Cat(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        @Override
        public String toString() {
            return "Cat [name=" + name + ", age=" + age + "]";
        }
        public Cat() {
            super();
            // TODO Auto-generated constructor stub
        }
        
    }
    public class ObjectStreamDemo {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            objectOut();
            objectIn();
        }
        //实现对象的序列化
        public static void objectOut(){
            Cat cat=new Cat("mimi",3);
            try {
                OutputStream out=new FileOutputStream("G://555.kds");
                ObjectOutputStream oos=new ObjectOutputStream(out);
                oos.writeObject(cat);
                oos.close();
                out.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //实现反序列化
        public static void objectIn(){
            try {
                InputStream in=new FileInputStream("G://555.kds");
                ObjectInputStream ois=new ObjectInputStream(in);
                Cat cat=(Cat)ois.readObject();
                System.out.println(cat);
                ois.close();
                in.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();
            }
        }
    }
  • 相关阅读:
    redis单机主从搭建
    zabbix监控rds
    zabbix_server表面启动成功,但是没有进程
    sysbench压测mysql
    使用gnuplot对tpcc-mysql压测结果生成图表
    tpcc-mysql的使用
    tpcc-mysql安装
    鼠标点击烟花爆炸效果
    css3背景自动变色代码
    js实现文本输入框的特效
  • 原文地址:https://www.cnblogs.com/shenhainixin/p/5118037.html
Copyright © 2011-2022 走看看