Java进阶之旅第十三天

Java进阶之旅第十三天

IO流

转换流

  • 是字符流和字节流之间的桥梁
  • 作用
    • 可以使得字节流使用字符流的方法
  • 在这里插入图片描述
构造方法
方法说明
public InputStreamReader(InputStream in)创建转换输入流对象
public OutputStreamWriter(OutputStream out)创建转换输出流对象
public InputStreamReader(InputStream in,String charset)创建转换输入流对象,并指定读取文件的编码方式
public OutputStreamWriter(OutputStream out,String charset)创建转换输出流对象,并指定读取文件的编码方式
指定编码方式读取文件内容
  • 方法一: 使用public InputStreamReader(InputStream in,String charset)
  • 方法二: 使用public FileReader(String pathname,Charset charset),其中调用Charset.forName(String charsetname)即可
  • 指定编码方式写出文件内容和以上方法基本一致
    • public OutputStreamWriter(OutputStream out,String charset)
    • public FileWriter(String pathname,Charset charset)
  • 代码
 		System.out.println("------------方式一");
        InputStreamReader isr = new InputStreamReader(new 		FileInputStream("a.txt"),"GBK");
        int len;
        while((len= isr.read())!=-1){
            System.out.print((char)len);
        }
        isr.close();
        System.out.println("\n------------方式二");
        FileReader fr = new FileReader("a.txt", Charset.forName("GBK"));
        int len2;
        while((len2= fr.read())!=-1){
            System.out.print((char)len2);
        }
        fr.close();
  • 结果
------------方式一
你好
不是
哥们
------------方式二
你好
不是
哥们
字节流使用字符流的方法
  • 使用public InputStreamReader(InputStream in)方法字节流读取任何数据都不会出现乱码,和字符流的特点一样
  • 再加之InputStreamReader()方法继承Reader,就可以带入BufferedReader(Reader r)方法中,使用字符缓冲流的两个特殊方法: newLine()和readLine()
  • 代码
		BufferedReader br = new BufferedReader(new InputStreamReader(new 			FileInputStream("a.txt"),"GBK"));
        String line;
        while ((line = br.readLine())!=null){
            System.out.print(line);
        }
        br.close();

序列化流

  • 可以将java中的对象写到本地文件当中,并且以特殊符号的方式进行记录不容易让人看懂以及无法使人轻易修改文件内容
构造方法和成员方法
方法说明
public ObjectOutputStream(OutputStream out)创建序列化流对象
public final void writeObject(Object obj)把对象序列化(写出)到文件中
  • 细节
    • 如果在运行过程中出现NotSerializableException异常
    • 解决方法: 让Javabean类实现Serializable接口(标记性接口)
  • 代码
//main代码
Student stu = new Student("小明", 12);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("info.txt"));
oos.writeObject(stu);
oos.close();
//Student类代码(部分
public class Student implements Serializable {
    String name;
    int age;
    ...
}
       
  • 结果
�� sr Student�
]�d^T I ageL namet Ljava/lang/String;xp   t 小明

反序列化流

  • 把序列化到本地文件中的对象,读取到程序中来
构造方法/成员方法
方法说明
public ObjectInputStream(InputStream in)创建序列化流对象
public Object readObject()把对象序列化从文件中读取出来
  • 细节

    • readObject()获取的对象需要进行强转后使用
  • 代码

		ObjectInputStream ios = new ObjectInputStream(new FileInputStream("info.txt"));
        Student stu = (Student)ios.readObject();
        System.out.println(stu);
        ios.close();
  • 结果
Student{name = 小明, age = 12}
重要细节(序列号
  • 序列号类型
    • 默认类型:java底层根据对象类的内容创建一个long类型的序列号,一旦修改对象就会重新计算序列号
    • 手动创建类型:在创建对象时,创建一个序列号(固定),修改对象不会重新计算序列号
  • 需要序列号的原因
    • 序列化的文件中的序列号,如果和反序列化中对象的序列号不一致就会报错,无法序列化。
  • 手动创建序列号
    • private static final long serialVersionUID = 数值
    • 格式固定,名称只能叫做serialVersionUID
选择性序列化
  • 有些时候,类中的某些属性我们不想让其序列化到本地文件
  • 方法: 添加transient瞬态关键字即可
    • private transient String name;
序列化多个对象
  • 如果我们需要序列化多个对象进入本地文件时,我们应该用ArrayList对象装载对象,然后序列化ArrayList对象即可
  • 除了ArrayList对象,大部分集合也适用(实现了java.io.Serializable接口
  • 代码
 	ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("info.txt"));
        Student stu1 = new Student("小米", 30);
        Student stu2 = new Student("小米", 30);
        Student stu3 = new Student("小米", 30);
        ArrayList<Student> list = new ArrayList<>();
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        oos.writeObject(list);
        oos.close();
  • 结果
�� sr java.util.ArrayListx����a� I sizexp   w   sr Student�
]�d^T I ageL namet Ljava/lang/String;xp   t 小米sq ~    q ~ sq ~    q ~ x
  • 21
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不吃牛肉!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值