IO流-4

1. 高效缓冲流

1.1 概述

  1. 对硬盘进行数据的读取相比于从内存中存取数据要慢的多。所以JDK为我们提供了高效缓冲流来提高我们IO流的效率。内部原理就是借助内存的缓冲区来减少硬盘IO的次数,提高性能。

1.2 分类

  • 字节流
    输入流
    BufferedInputStream
    输出流
    BufferedOutputStream

  • 字符流
    输入流
    BufferedReader
    输出流
    BufferedWriter

1.2 对象的创建

构造方法:

  1. public BufferedInputStream(InputStream in)
  2. public BufferedOutputStream(OutputStream out)
  3. public BufferedReader(Reader in)
  4. public BufferedWriter(Writer out)

范例:

  1. public static void main(String[] args) throws IOException {
  2. BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\root\\Desktop\\test\\汉字.txt"));
  3. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\root\\Desktop\\test\\汉字3.txt"));
  4. BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\root\\Desktop\\test\\汉字3.txt"));
  5. BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\root\\Desktop\\test\\汉字3.txt"));
  6. }

1.3 特有的方法

类 方法 作用
BufferedReader public String readLine() throws IOException 一次读取一行数据,读到的内容不包含换行符,读到了文件末尾返回null。
BufferedWriter public void newLine() throws IOException 写入一个换行符,会根据系统变化

范例:

  1. public static void main(String[] args) throws IOException {
  2. //readLine
  3. //创建对象
  4. BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\root\\Desktop\\test\\333.txt"));
  5. //读取数据
  6. String line;
  7. while((line=br.readLine())!=null){
  8. //把读到的内容输出
  9. System.out.println(line);
  10. }
  11. //释放资源
  12. br.close();
  13. }
  1. public static void main(String[] args) throws IOException {
  2. //newLine
  3. //创建对象
  4. BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\root\\Desktop\\test\\444.txt"));
  5. //写入数据
  6. bw.write("你好啊");
  7. //写入换号符
  8. bw.newLine();
  9. bw.write("我很好");
  10. //释放资源
  11. bw.close();
  12. }
  1. public static void main(String[] args) throws IOException {
  2. //newLine
  3. //创建对象
  4. BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\root\\Desktop\\test\\333.txt"));
  5. BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\root\\Desktop\\test\\444.txt"));
  6. //循环读写数据 把读到的数据写入目标文件中
  7. String line;
  8. while((line=br.readLine())!=null){
  9. //把读到的数据写入文件
  10. bw.write(line);
  11. //写入换行符
  12. bw.newLine();
  13. }
  14. //释放资源
  15. br.close();
  16. bw.close();
  17. }

1.4 应用场景

  1. 如果想让IO操作效率更高或者想使用特有的方法(readLine、newLine)就可以使用高效缓冲流。

2. 转换流

2.1 概述

  1. 如果我们需要把字节流转换成字符流,可以使用转换流来实现转换。

2.2 分类

流类型 类
输入流 InputStreamReader
输出流 OutputStreamWriter

2.2 转换流的使用

当我们需要把字节流转换成字符流时直接使用转换流的构造方法进行转换即可。

  1. public InputStreamReader(InputStream in)
  2. public InputStreamReader(InputStream in, String charsetName)
  3. public OutputStreamWriter(OutputStream out)
  4. public OutputStreamWriter(OutputStream out, String charsetName)

范例:

  1. public static void main(String[] args) throws FileNotFoundException {
  2. //字节流对象
  3. FileInputStream fis = new FileInputStream("C:\\Users\\root\\Desktop\\test\\汉字.txt");
  4. FileOutputStream fos = new FileOutputStream("C:\\Users\\root\\Desktop\\test\\转换流测试.txt");
  5. //转换成字符流
  6. InputStreamReader isr = new InputStreamReader(fis);
  7. OutputStreamWriter osw = new OutputStreamWriter(fos);
  8. }

3. Properties

  1. Properties 其实是一个Map集合,其Key和Value都是String类型。他提供了和流结合的方法,可以方便我们把集合中的数据写入文件或者是把文件中的数据读取到集合中。

3.1 对象创建

构造方法:

  1. public Properties()

范例:

  1. public static void main(String[] args) {
  2. Properties properties = new Properties();
  3. }

3.2 常用方法

  1. Map集合的方法Properties都有,这里不做演示。因为我们在使用Properties的时候一般都是使用其特有的一些方法。

常用方法:

  1. public synchronized Object setProperty(String key, String value) //设置键值对
  2. public String getProperty(String key) //根据键获取对应的值
  3. public Set<String> stringPropertyNames()//获取所有Key的集合

范例:

  1. public static void main(String[] args) {
  2. Properties properties = new Properties();
  3. //存储key value
  4. properties.setProperty("name","三更");
  5. properties.setProperty("age","17");
  6. //获取对应的值
  7. String v = properties.getProperty("name");
  8. Set<String> keys = properties.stringPropertyNames();
  9. for (String key : keys) {
  10. String value = properties.getProperty(key);
  11. System.out.println(key+"===="+value);
  12. }
  13. }

3.2 和流结合的方法

  1. public synchronized void load(Reader reader) throws IOException //通过字符流加载数据
  2. public synchronized void load(InputStream inStream) throws IOException //通过字节流加载数据
  3. public void store(Writer writer, String comments) throws IOException //通过字符流保存数据
  4. public void store(OutputStream out, String comments) throws IOException//通过字节流保存数据

范例:

  1. public static void main(String[] args) throws IOException {
  2. Properties pro = new Properties();
  3. //存数据
  4. pro.setProperty("name","三更");
  5. pro.setProperty("age","16");
  6. //把集合中的数据写入文件
  7. FileWriter fw = new FileWriter("C:\\Users\\root\\Desktop\\test\\proTest.txt");
  8. pro.store(fw,"java");
  9. fw.close();
  10. }
  1. public static void main(String[] args) throws IOException {
  2. //创建集合
  3. Properties pro = new Properties();
  4. //从文件中加载数据
  5. FileReader fr = new FileReader("C:\\Users\\root\\Desktop\\test\\proTest.txt");
  6. pro.load(fr);
  7. }

3.3 应用场景

  1. Properties主要是用来读取和写入配置文件时使用。要求配置文件中的数据格式是: key=value