木舟0基础学习Java的第十九天(装饰设计模式,转换流,对象操作流(序列化),Properties集合)

装饰设计模式

创建一个接口 用一个类实现接口 再创建一个类实现这个接口 第二个类中包含第一个类中的方法 和自己的方法 还可以增强

案例:

public interface Car {
    public void run();
    public void carry();
}
public class Taxi implements Car{
    @Override
    public void run() {
        System.out.println("正常行驶");
    }

    @Override
    public void carry() {
        System.out.println("载人");
    }
}
public class Tank implements Car{
    private Taxi t;

    public Tank(Taxi t) {
        this.t = t;
    }

    @Override
    public void run() {
        t.run();
        System.out.println("高速行驶");
    }

    @Override
    public void carry() {
        t.carry();
        System.out.println("载军人");
    }
    //增强方法
    public void launch(){
        System.out.println("发射炮弹");
    }
    public void defense(){
        System.out.println("防御子弹");
    }
}
public class Test {
    public static void main(String[] args) {
        Taxi t=new Taxi();
        t.run();
        t.carry();
        System.out.println("------------------------------------");
        //增强之后
        Tank tk=new Tank(t);//相当于在Taxi t 包装成一个类
        tk.run();
        tk.carry();
        tk.launch();
        tk.defense();
    }
}

转换流(不推荐拷贝)

使用指定的码表来读写字符

public static void main(String[] args) {
        FileInputStream in=null;
        InputStreamReader isr=null;
        FileOutputStream out=null;
        OutputStreamWriter osw=null;
        try {
            //读
            in=new FileInputStream("e:/abc/a.txt");
            //按指定编码解码 gbk必须与文档编码一致 不然编译不出来
            isr=new InputStreamReader(in,"gbk");
            //写
            out=new FileOutputStream("e:/cba/b.txt");
            osw=new OutputStreamWriter(out,"utf-8");
            int len=0;
            while((len=isr.read())!=-1){
                osw.write(len);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(osw!=null){
                try {
                    osw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(isr!=null){
                try {
                    isr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

对象操作流

对象序列化:使用一个字节序列 表示一个对象

字节序列包含:对象类型 数据 存储的属性等信息...

序列化流:ObjectOutputStream,ObjectInputStream

将Java对象的原始数据和图像写入 通过ObjectOutputStream读取对象 通过使用流的文件来实现对象的持久存储

对象序列化必须实现Serializable接口

public class User implements Serializable {
    //对象序列化必须实现Serializable
    private String username;
    private String password;

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

序列化

 public static void main(String[] args) {
        //序列化
        User user=new User("muzhou","z.05250525");
        ObjectOutputStream oos=null;
        try {
            oos=new ObjectOutputStream(new FileOutputStream("b.txt"));
            oos.writeObject(user);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

反序列化

public static void main(String[] args) {
        //反序列化
        ObjectInputStream ois=null;
        try {
            ois=new ObjectInputStream(new FileInputStream("b.txt"));
            User o = (User)ois.readObject();
            System.out.println(o);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }finally{
            if(ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

如果序列化后对对象进行修改 再进行反序列化 可能会报错 InvalidClassException 这时我们需要重新序列化

第一步:给对象所属的类加一个UID 例:private static final long serialVersionUID=1L;

第二步:序列化

第三步:反序列化

如果某个成员变量不想被序列化 就在该成员变量前加上 transient关键字

写入和读取多个对象案例:

public static void main(String[] args) {
        //写入和读取多个对象
        ObjectOutputStream oos=null;
        ObjectInputStream ois=null;
        Student stu1=new Student("张三",23);
        Student stu2=new Student("李四",24);
        Student stu3=new Student("王五",25);

        try {
            //写
            oos=new ObjectOutputStream(new FileOutputStream("c.txt"));
            oos.writeObject(stu1);
            oos.writeObject(stu2);
            oos.writeObject(stu3);
            //读
            ois=new ObjectInputStream(new FileInputStream("c.txt"));
            Object o;
            while(true){
                try{
                    o= ois.readObject();
                    System.out.println(o);
                }catch(EOFException e){
                    break;
                }

            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally{
            if(ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
通过List集合写入和读取多个对象案例:推荐使用
 public static void main(String[] args) {
        User u1=new User("张三","123");
        User u2=new User("李四","456");
        User u3=new User("王五","789");
        ObjectOutputStream oos=null;
        ObjectInputStream ois=null;
        ArrayList<User> list=new ArrayList();
        try {
            oos=new ObjectOutputStream(new FileOutputStream("d.txt"));
           //往本地文件中写入的就是一个集合
            list.add(u1);
            list.add(u2);
            list.add(u3);
            oos.writeObject(list);
            ois=new ObjectInputStream(new FileInputStream("d.txt"));
            ArrayList<User> list1=(ArrayList<User>) ois.readObject();
            for (User user : list1) {
                System.out.println(user);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }finally{
            if(ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

Properties集合

Properties集合可以保存到流中或者从流中加载

Properties集合可以作为Map集合使用(键值对) 他是Map体系的集合类

使用:存储和遍历

方法1

 public static void main(String[] args) {
        Properties p=new Properties();
        //存储
        p.setProperty("01","张三");
        p.setProperty("02","李四");
        p.setProperty("03","王五");
        //遍历集合
        Set<String> names=p.stringPropertyNames();//根据键取值
        for (String key : names) {
            String value = p.getProperty(key);
            System.out.println("键"+key+"值"+value);
        }
    }

方法2

  public static void main(String[] args) {
        //Properties<String,String> p=new Properties();错误写法
        Properties p=new Properties();
        //存储
        p.put("01","张三");
        p.put("02","李四");
        p.put("03","王五");
        //遍历集合
        Set<Object> keySet=p.keySet();//根据键取值
        for (Object key : keySet) {
            Object value = p.get(key);
            System.out.println("键"+key+"值"+value);
        }
    }
Properties和流(方法 load store)
 public static void main(String[] args) {
        store();
        load();
    }
    public static void load(){
        Properties pr=new Properties();
        FileReader fr=null;
        try {
           fr=new FileReader("e:/abc/b.txt");
            pr.load(fr);
            System.out.println(pr);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(fr!=null){
                try {
                    fr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    public static void store(){
        Properties pr=new Properties();
        FileWriter fw=null;
        pr.setProperty("张三","23");
        pr.setProperty("李四","24");
        pr.setProperty("王五","25");
        try {
            fw=new FileWriter("e:/abc/b.txt");
            pr.store(fw,null);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(fw!=null){
                try {
                    fw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

  • 18
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值