字符流:

转换流:

字符输入流

  1. 字符输入流转换流InputStreamReader作用
    1. 构造方法 public InputStreamReader(InputStream is,String charset);
    2. 可以解决由不同编码的文件读取时,解决乱码问题
    3. 可以指定编码把原始字节流转换成字符流,如此字符流中的字符不会乱码
  2. 代码:

    1. package com.hhm.h_tranfer_stream;
    2. import java.io.*;
    3. /* 转换流
    4. *
    5. * */
    6. public class CharSetTest001 {
    7. public static void main(String[] args) {
    8. /*
    9. *
    10. * 文件是GBK IDEA是UTF-8
    11. * */
    12. try (
    13. //资源流的对象
    14. //1、创建一个输入流
    15. /*转换流 :InputStreamReader public InputStreamReader(InputStream is) */
    16. InputStream in = new FileInputStream("F:\\testFile\\out.txt");
    17. //把原始字节流转换为字符输入流
    18. InputStreamReader ir = new InputStreamReader(in, "GBK");
    19. ) {
    20. int len;
    21. while ((len = ir.read()) != -1) {
    22. System.out.print((char) len);
    23. }
    24. } catch (Exception e) {
    25. e.printStackTrace();
    26. }
    27. }
    28. }

    字符输出流

  3. 字符输出流OutputStreamWriter

    1. 构造方法OutputStreamWriter(OutputStream os,String charset);
    2. 可以指定编码把字节输出流转换成字符输出流,从而可以指定写出去的字符编码

代码:

  1. import java.io.*;
  2. /* 转换流
  3. * 字符输出流
  4. * */
  5. public class CharSetOut {
  6. public static void main(String[] args) throws Exception {
  7. //定义字节输出流
  8. OutputStream os = new FileOutputStream("F:\\testFile\\out.txt",true);
  9. OutputStreamWriter ow = new OutputStreamWriter(os,"GBK");
  10. //把低级的流 再做一次包装 做成高级缓冲流
  11. BufferedWriter bw = new BufferedWriter(ow);
  12. bw.write("OutputStreamWriter输出流");
  13. bw.close();
  14. }
  15. }

对象序列化

  • 作用:以内穿为基准,把内存中的对象存储到磁盘文件中去,称为对象序列化。
    • 构造方法:public ObjectOutputStream(OutputStream out);把低级字节流包装成高级对象字节流输出流
    • 将对象写入的磁盘当中
    • 对象字节输出流,写对象数据到磁盘
  • 使用到的流是对象字节输出流:ObjectOutputStream
  • 使用序列化存储对象,对象类必须实现Serializable接口否则,就会抛出一个没有序列化的错误

    1. public static void main(String[] args) {
    2. //1、创建一个学生类对象
    3. Student s = new Student("张国荣",25);
    4. Student s1 = new Student("张学友",25);
    5. Student s2 = new Student("陈冠希",25);
    6. try (
    7. //2、创建低级流
    8. OutputStream os = new FileOutputStream("test.txt");
    9. //3、对象序列化:使用对象字节输出流包装字节输出流管道
    10. ObjectOutputStream oos = new ObjectOutputStream(os);
    11. ){
    12. //3、直接调用序列化方法 想调用序列化方法必须让Student类实现一个接口
    13. oos.writeObject(s);
    14. //释放资源
    15. oos.close();
    16. } catch (IOException e) {
    17. e.printStackTrace();
    18. }
    19. }
    20. Student类:
    21. public class Student implements Serializable {
    22. private String name;
    23. private transient Integer age;
    24. public Student(String name, Integer age) {
    25. this.name = name;
    26. this.age = age;
    27. }
    28. public String getName() {
    29. return name;
    30. }
    31. public void setName(String name) {
    32. this.name = name;
    33. }
    34. public Integer getAge() {
    35. return age;
    36. }
    37. public void setAge(Integer age) {
    38. this.age = age;
    39. }
    40. }

    总结:

  • 序列化的含义是什么?

    • 把对象存储到文件中去
  • 对象序列化用到了哪个流?
    • 对象字节输出流ObjectOutputStream
    • public void writeObject(Object obj)
  • 序列化对象的要求是怎么样的?

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

      对象反序列化

  • 使用到的流是对象字节输入流:ObjectInputStream

  • 作用:以内存为基准,把存储到磁盘文件中去的对象数据恢复成内存中的对象,称为对象反序列化
  • 构造器:public ObjectInputStream(InputStream in);把低级字节流包装成高级对象字节输入流
  • 序列化方法:public Object readObject();把存储在磁盘文件中的对象数据恢复成内存中的对象返回
  • transient这个修饰符是不让这个属性写入序列化文件

    1. public class ObjcetInputStreamDemo01 {
    2. public static void main(String[] args) {
    3. try (
    4. //2、创建低级流
    5. InputStream os = new FileInputStream("test.txt");
    6. //3、对象序列化:使用对象字节输入流包装字节输出流管道
    7. ObjectInputStream oi = new ObjectInputStream(os);
    8. ){
    9. //3、直接调用反序列化方法 单个序列化取出方法 我在里面存了三个序列化对象 调用了三次
    10. Student st = (Student) oi.readObject();
    11. Student st1 = (Student) oi.readObject();
    12. Student st2 = (Student) oi.readObject();
    13. System.out.println(st.getName()+" "+st.getAge());
    14. System.out.println(st1.getName()+" "+st1.getAge());
    15. System.out.println(st2.getName()+" "+st2.getAge());
    16. } catch (Exception e) {
    17. e.printStackTrace();
    18. }
    19. }
    20. }
    21. student类:
    22. public class Student implements Serializable {
    23. private String name;
    24. //transient修饰的成员变量不参与序列化
    25. private transient Integer age;
    26. public Student(String name, Integer age) {
    27. this.name = name;
    28. this.age = age;
    29. }
    30. public String getName() {
    31. return name;
    32. }
    33. public void setName(String name) {
    34. this.name = name;
    35. }
    36. public Integer getAge() {
    37. return age;
    38. }
    39. public void setAge(Integer age) {
    40. this.age = age;
    41. }
    42. }

    打印流

  • 作用:打印流可以实现方便、高效的打印数据到文件中去。打印流一般是指:PrintStream,PrintWriter两个类

  • 可以实现打印说明数据就是什么数据
  • 构造器:

    • public PrintStream(OutputStream os);打印流直接通向字节输出流管道
    • public PrintStream(File f);打印流直接通向文件对象
    • public PrintStream(String filepath);打印流直接通向文件路径
    • 打印流构造器支持编码
    • 如果要做追加操作就用第一个构造器

      1. package com.hhm.h_print_stream;
      2. import java.io.FileNotFoundException;
      3. import java.io.FileOutputStream;
      4. import java.io.PrintStream;
      5. /*
      6. *
      7. * 学会使用PrintStream 打印流 高效 方便
      8. * */
      9. public class PrintStreamDemo {
      10. public static void main(String[] args) {
      11. //1、创建一个打印流对象
      12. try (
      13. PrintStream pr = new PrintStream("test.txt");
      14. ) {
      15. //内容写什么 文件就得到什么
      16. pr.print(97);
      17. pr.close();
      18. } catch (FileNotFoundException e) {
      19. e.printStackTrace();
      20. }
      21. }
      22. }

      PrintWriter

  • 构造器:

    • public PrintStream(OutputStream os);打印流直接通向字节输出流管道
    • public PrintStream(File f);打印流直接通向文件对象
    • public PrintStream(String filepath);打印流直接通向文件路径

PrintWriter与PrintStream区别

  • 打印数据功能上是一模一样,都是使用方便性能高效(核心优势)
  • PrintStream继承自字节输出流OutputStream,支持写字节数据的方法
  • PrintWriter继承自字符输出流Writer,支持字符数据
  • 如果要使用写数据writer()方法 就只能使用PrintWriter,没有必要毕竟这是打印流

改变输出语句的位置到文件

  1. //重定向
  2. class PrintDemo2{
  3. //print原理就是调用的就是打印流
  4. //System.out.println();
  5. public static void main(String[] args){
  6. System.out.println("锦瑟无端五十弦"); //流到控制台
  7. System.out.println("一弦一柱思华年"); //流到控制台
  8. //我们使用打印流改变输出语句的位置
  9. PrintStream ps = new PrintStream("test.txt");
  10. System.setOut(ps); //把系统打印流改成我们自己的打印流
  11. System.out.println("庄生晓梦迷蝴蝶"); //流到文件中
  12. System.out.println("望帝春心托杜鹃"); //流到文件中
  13. }
  14. }

Properties

  • 其实就是一个Map集合,但是我们一般不会当集合使用,因为HashMap更好用

Properties核心作用

  • 代表的是一个属性文件,可以把自己2对象中的键值对信息存入到一个属性文件中去
  • 属性文件:后缀是.properties结尾的文件,里面内容都是key=value,后续做系统配置信息
  • 构造器:properties();

存储

  1. public class h_propertiesDemo {
  2. public static void main(String[] args) throws Exception {
  3. Properties properties = new Properties();
  4. // properties.put("admin","123456");
  5. //不使用put方法 使用setProperty方法
  6. properties.setProperty("admin","123456");
  7. //存储到文件当中去
  8. //可以存储到低级的字符输出流里面 也可以选择存入低级字符输出流里面 两种没有区别
  9. /*
  10. * 参数一:流管道
  11. * 参数二:可写可不写 一个注释 comments
  12. * */
  13. properties.store(new FileWriter("test.properties"),"give me 300 thank you ");
  14. }
  15. }

读取

  1. public class PropertiesLoad {
  2. /*
  3. * 加载数据
  4. *
  5. * */
  6. public static void main(String[] args) throws Exception {
  7. Properties properties = new Properties();
  8. properties.load(new FileReader("test.properties"));
  9. System.out.println(properties);
  10. }
  11. }
  12. properties中的get方法单独取数据
  13. public static void main(String[] args) throws Exception {
  14. Properties properties = new Properties();
  15. properties.load(new FileReader("test.properties"));
  16. //当properties文件中有多个 key和值我们就得取 properties提供了get方法直接get键
  17. //无非就调用的是get 但是他帮你重写了所以可以得到String类型
  18. String admin = properties.getProperty("admin");
  19. System.out.println(admin);
  20. }
  21. }

实现原理:
从文件中获取到数据,然后放到properties这个集合当中然后输出

IO框架

Commons-io概述

  • Commons-io是Apache开源基金组织提供的一组有关IO操作的类库,可以提高IO功能开发效率。
  • commons-io工具包提供了很多有关IO的操作类。有两个主要的类FileUtils,IOUtils

    1. public class CommonsIoDemo01 {
    2. public static void main(String[] args) throws IOException {
    3. //1、完成文件复制到某一个文件夹下
    4. FileUtils.copyFileToDirectory(new File("F:\\testFile\\test.txt"),new File("F:"));
    5. //2、完成文件复制
    6. IOUtils.copy(new FileInputStream("F:\\testFile\\test.txt"),
    7. new FileOutputStream("F:\\testFile\\test2.txt"));
    8. //3、完成文件夹复制到某个文件下
    9. FileUtils.copyDirectoryToDirectory(new File("F:\\testFile"),new File("D:\\newTestFile"));
    10. //4、删除某个文件夹
    11. FileUtils.deleteDirectory(new File("F:\\newTestFile"));
    12. }
    13. }

    JDK1.7原生的 后newIo 新Io

    1. //完成文件的复制
    2. Files.copy(Path.of("F:\\testFile\\test.txt"),Path.of("F:\\testFile\\test2.txt"));
    3. //只能删空文件夹 第三方可以删除有文件的文件夹
    4. Files.deleteIfExists(Path.of("F:\\testFile"));