13. IO流

作者: 星野君 | 来源:发表于2022-04-29 10:28 被阅读0次

一、文件
创建文件:

public class Home {
  public static void main(String[] args) {
    String filePath = "e:\\news1.txt";
    File file = new File(filePath);
    try {
      file.createNewFile();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

二、IO流分类

image.png
image.png

三、节点流FileInputStream
文件字节输入流:

public class Home {
  public static void main(String[] args) {
    String filePath = "e:\\a.txt";
    byte[] buf = new byte[8];
    int readLen = 0;
    FileInputStream fileInputStream = null;
    try {
      fileInputStream = new FileInputStream(filePath);
      while ((readLen = fileInputStream.read(buf)) != -1) {
        System.out.print(new String(buf, 0, readLen));
      }

    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        fileInputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

四、节点流FileOutputStream
文件字节输出流:

public class Home {
  public static void main(String[] args) {
    String filePath = "e:\\a.txt";
    FileOutputStream  fileOutputStream =null;
    try {
      // 参数加true是追加内容,不加是覆盖
      fileOutputStream = new FileOutputStream(filePath);
      String str = "hello,world!";
      fileOutputStream.write(str.getBytes());
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        fileOutputStream .close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

四、节点流FileReader
文件字符输入流:

public class Home {
  public static void main(String[] args) {
    String filePath = "e:\\a.txt";
    FileReader fileReader = null;
    int readLen = 0;
    char[] buf = new char[8];
    try {
      fileReader = new FileReader(filePath);
      while ((readLen = fileReader.read(buf)) != -1) {
        System.out.print(new String(buf, 0, readLen));
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        fileReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

五、节点流FileWriter
文件字符输出流:

public class Home {
  public static void main(String[] args) {
    String filePath = "e:\\a.txt";
    FileWriter fileWriter = null;
    try {
      fileWriter = new FileWriter(filePath);
      fileWriter.write(" 你好北京~");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        fileWriter.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

六、处理流-BufferedReader
字符流输入流:

public class Home {
  public static void main(String[] args) {
    String filePath = "e:\\a.txt";
    String line;
    BufferedReader bufferedReader = null;
    try {
      bufferedReader = new BufferedReader(new FileReader(filePath));
      while ((line = bufferedReader.readLine()) != null) {
        System.out.println(line);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        bufferedReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

七、处理流-BufferedWriter
字符流输出流:

public class Home {
  public static void main(String[] args) {
    String filePath = "e:\\a.txt";
    BufferedWriter bufferedWriter = null;
    try {
      bufferedWriter = new BufferedWriter(new FileWriter(filePath));
      bufferedWriter.write("哈哈哈");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        bufferedWriter.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

八、处理流-BufferedInputStream
字节输入流:

public class Home {
  public static void main(String[] args) {
    String filePath = "e:\\a.txt";
    BufferedInputStream bufferedInputStream = null;
    byte[] buff = new byte[1024];
    try {
      bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));
      while (bufferedInputStream.read(buff) != -1) {
        System.out.println(new String(buff));
      }

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        bufferedInputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

九、处理流-BufferedOutputStream
字节输出流:

public class Home {
  public static void main(String[] args) {
    String filePath = "e:\\a.txt";
    BufferedOutputStream bufferedOutputStream = null;
    try {
      bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath));
      bufferedOutputStream.write("xxx".getBytes());
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        bufferedOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

十、对象流-ObjectInputStream

public class Home {
  public static void main(String[] args) {
    String filePath = "e:\\a.dat";
    ObjectInputStream objectInputStream = null;
    try {
      objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
      System.out.println(objectInputStream.readObject()); // 这里返回的是object对象

    } catch (IOException | ClassNotFoundException e) {
      e.printStackTrace();
    } finally {
      try {
        objectInputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

class Cat implements Serializable {
  private static final long serialVersionUID = 1L;
  private String name;

  public Cat(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

十一、对象流ObjectOutputStream

public class Home {
  public static void main(String[] args) {
    String filePath = "e:\\a.dat";
    ObjectOutputStream oos = null;
    try {
      oos = new ObjectOutputStream(new FileOutputStream(filePath));
      oos.writeObject(new Cat("小黄"));
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        oos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

class Cat implements Serializable {
  private static final long serialVersionUID = 1L;
  private String name;

  public Cat(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}
  1. 序列化的类必须实现Serializable接口
  2. 读写顺序要一致
  3. 序列化中的类建议添加

十二、Properties
Properties是专门用于读写配置文件的集合类。格式是key=value

常见方法:

  1. load 加载数据到properties对象
  2. list 将数据显示到指定设备
  3. getProperty(key) 根据key获取到value
  4. setProperty(key, value)设置键值对到properties对象
  5. store 将properties对象保存的键值对存储到配置文件中
//1. 创建 Properties 对象
Properties properties = new Properties();
//2. 加载指定配置文件
properties.load(new FileReader("src\\mysql.properties"));
//3. 把 k-v 显示控制台
properties.list(System.out);
//4. 根据 key 获取对应的值
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");

相关文章

  • 13. IO流

    一、文件创建文件: 二、IO流分类 三、节点流FileInputStream文件字节输入流: 四、节点流FileO...

  • java基础-day20-IO流和StringBuffer

    IO流和StringBuffer 1. IO流 1.1 IO流概述 1.2 IO流分类 1.3 文件操作输入输出字...

  • Java之IO流详解

    title: Java之IO流详解tags: Java IO流categories: Java IO流 大多数应用...

  • 15_IO流

    IO流介绍 IO流是一个流水模型:IO理解成水管,把数据理解成水流 IO流的分类:按照流的方向分为:输入流、输出流...

  • IO流

    一、IO流 1.1 IO的概述 IO流介绍 IO:输入/输出(Input/Output) 流:是一种抽象概念,是对...

  • java io 流

    java io 流 io 流总览 io 流主要提供四个接口 InputStream: 输入字节流 OutputSt...

  • Java IO流(一)

    IO(Input Output)流的概述 下面给出IO流的基本概述,这样可以对IO流有一个宏观上的基本了解。 IO...

  • IO流

    一、IO流的概述: 二、IO流的分类: 三、字节缓冲流: 四、字符缓冲流: 五、转换流(把字节流转换为字符流): ...

  • IO流

    IO流的使用 今天咱们来说一下JAVA中最常用的数据处理的流 IO 流,说到IO啊,玩DOTA的小伙伴们就有声音...

  • IO流

    大家好,我是IT修真院深圳分院java第4期学员,一枚正直善良的java程序员。今天给大家分享一下,Java简单的...

网友评论

      本文标题:13. IO流

      本文链接:https://www.haomeiwen.com/subject/fklysrtx.html