前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java学习day12--IO对比,序列化

java学习day12--IO对比,序列化

作者头像
曼路
发布2018-10-18 15:29:39
3660
发布2018-10-18 15:29:39
举报
文章被收录于专栏:浪淘沙浪淘沙浪淘沙

2018.6.19 1.IO 字节流 InputStream OutputStream 字符流 Reader Writer

 字节流
    FileInputStream in = new FileInputStream("d:/b.txt");
    int i1 = in.read();
    int i2 = in.read();
    int i3 = in.read();
    String s =new String(new byte[] {(byte) i1,(byte) i2,(byte) i3});
    System.out.println(s);
    byte[] b = new byte[10];
    in.read(b);
    String str = new String(b);
    System.out.println(str);

字节缓冲流 缓冲流一定要flush() 否则不能写入

   BufferedInputStream in = new BufferedInputStream(new FileInputStream(""));
   BufferedOutputStream out = new BufferedOutputStream(new FileOutputStrea(""));
   byte[] b = new byte[1024];
   int len;
   while((len=in.read())!=-1){
    out.write(b,0,len);
    out.flush();
}
 转换流:
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d:/a.txt"));
    InputStreamReader in = new InputStreamReader(new FileInputStream("d:/a.txt"));

    out.write("哈哈\n我喜欢你走在我的身旁,永远会在你的身边");
    char[] c = new char[1];
    int len;
    while((len=in.read(c))!=-1) {
        System.out.print(c);
        }
    out.close();
    in.close();
 字符缓冲流
       BufferedReader br = new BufferedReader(new FileReader("d:/a.txt"));
    BufferedWriter w = new BufferedWriter(new FileWriter("d:/b.txt") );
    char[] c = new char[10];
    br.read(c);
    System.out.println(c);
    w.write(c);
    br.close();
    w.close();

2.编码 utf-8 3个字节一个汉字 gbk 2个字节一个汉字 乱码的原因:编码和解码的方式不一致。


3.String和Byte String转byte byte[] b = s.getBytes(); byte转String String s = new String(byte);


4.序列化 Java序列化是指把Java对象转换为字节序列的过程

Java反序列化是指把字节序列恢复为Java对象的过程

注意:public class People implements Serializable

  ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("d:/a.txt"));
    People p = new People(12,"ahha");
    out.writeObject(p);
    out.close();

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("d:/a.txt"));
    Object o = in.readObject();
    System.out.println(o);
    in.close();

5.Properties集合 Properties概述 属性 作为配置文件用 Properties 作为Map集合的使用

Properties p = new Properties();
    p.load(new FileInputStream("./conf/bean.properties"));
    String name = p.getProperty("name");
    System.out.println(name);

6.计算程序运行时间

long l = system.currentTimeMillis();
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年06月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档