字节流

对象流

  • 增强了缓冲区功能
  • 增强了读写8种基本数据类型和字符串功能
  • 增强了读写对象的功能
    1)readObject()从流中读取一个对象 反序列化
    2)writeObject()向流中写入一个对象 序列化
  • 注意事项:
    1)序列化类必须要实现Serializable接口
    2)序列化类中对象属性要求实现Serializable接口
    3)序列化版本号ID,是保证序列化的类和反序列化的类是同一个类
    4)使用transient修饰属性,这个属性不能序列化
    5)静态属性不能序列化
    6)序列化多个对象可以借助集合实现
  • ObjectInputStream

  • 反序列化
public class Test {
    public static  void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("d:\\stu.bin");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Student s = (Student) ois.readObject();
        ois.close();
        System.out.println("读取完毕");
        System.out.println(s.toString());
    }
}
  • ObjectOutputStream

  • 序列化的类必须实现Serializable声明接口
public class Student implements Serializable {//要想将对象序列化必须实现Serializable声明接口
    private String name;
    private int age;
public class Test {
    public static  void main(String[] args) throws Exception {
        //1.要创建对象流必须先创建一个节点流
        FileOutputStream fos = new FileOutputStream("d:\\stu.bin");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        //2.序列化
        Student s1 = new Student("张三",19);
        oos.writeObject(s1);
        //3.关闭
        oos.close();
        System.out.println("序列化完毕");
    }
}

字符流

字符编码

UTF-8 针对Unicode码表的可变长度字符编码,一个汉子3个字节
当编码方式和解码方式不一致时,会出现乱码

字符流的父类(抽象类)

  • Reader:字符输入流

文件字符流:FileReader(文件字符输入流)

public class Test {
    public static  void main(String[] args) throws Exception {
        //创建文件字符输入流
        FileReader fr = new FileReader("d:\\aaa.txt");
        //单个字符读取
        int date = 0;
        while ((date=fr.read())!=-1){//一次读取一个字符,即三个字节
            System.out.print((char)date);
        }
        //创建字符缓冲区,一次读取多个字符
        char[] buf = new char[1024];
        int count = 0;
        while ((count = fr.read(buf))!=-1){
            System.out.println(new String(buf,0,count));
        }
        fr.close();
    }
}
  • Writer:字符输出流

文件字符流:FileWriter(文件字符输出流)

public class Test {
    public static  void main(String[] args) throws Exception {
        //创建文件字符输出流
        FileWriter fw = new FileWriter("d:\\hello.txt");
        fw.write("张三是好人");
        fw.flush();
        fw.close();
    }
}

字符流复制文件

  • 用FileReader和FileWriter只能复制文本文件,不能复制图片或二进制文件
public class Test {
    public static  void main(String[] args) throws Exception {
        FileReader fr = new FileReader("d:\\hello.txt");
        FileWriter fw = new FileWriter("d:\\copy.txt");
        char[] buf = new char[1024];
        int count = 0;
        while ((count=fr.read(buf))!=-1){
            fw.write(new String(buf,0,count));
            fw.flush();
        }
        fr.close();
        fw.close();
    }
}

字符缓冲流

  • 字符缓冲输入流:BufferedReader

public class Test {
    public static  void main(String[] args) throws Exception {
        FileReader fr = new FileReader("d:\\hello.txt");
        BufferedReader br = new BufferedReader(fr);
        int date = 0;
        while ((date=br.read())!=-1){
            System.out.println((char) date);
        }
        //2.readLine读取一行
        String line = null;
        while ((line = br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
    }
}
  • 字符缓冲输出流:BufferedWriter

public class Test {
    public static  void main(String[] args) throws Exception {
        FileWriter fw = new FileWriter("d:\\hello.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("张三是好人");
        bw.newLine();//换行
        bw.write("你也是");
        bw.flush();
        bw.close();
        System.out.println("执行完毕");
    }
}

打印流

PrintWriter

public class Test {
    public static  void main(String[] args) throws Exception {
        PrintWriter pw = new PrintWriter("d:\\hello.txt");
        pw.println(97);
        pw.println('a');
        pw.println("张三");
        pw.close();
        System.out.println("执行完毕");
    }
}

转换流

  • 可将字节流转换为字符流
  • 可设置字符的编码方式

InputStreamReader

public class Test {
    public static  void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("d:\\hello.txt");
        InputStreamReader isr = new InputStreamReader(fis,"utf-8");
        int date = 0;
        while ((date = isr.read())!=-1){
            System.out.print((char) date);
        }
        isr.close();
    }
}

OutputStreamWriter

public class Test {
    public static  void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("d:\\hello.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
        osw.write("张三是个好学生");
        osw.flush();
        osw.close();
        System.out.println("执行完毕");
    }
}