# 44-IO流-4

IO流-4

1. 高效缓冲流

1.1 概述

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

1.2 分类

  • 字节流

    输入流

    ​ BufferedInputStream

    输出流

    ​ BufferedOutputStream

  • 字符流

    输入流

    ​ BufferedReader

    输出流

    ​ BufferedWriter

1.2 对象的创建

构造方法:

public BufferedInputStream(InputStream in) 
public BufferedOutputStream(OutputStream out)
public BufferedReader(Reader in) 
public BufferedWriter(Writer out)

范例:

    public static void main(String[] args) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\root\\Desktop\\test\\汉字.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\root\\Desktop\\test\\汉字3.txt"));


        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\root\\Desktop\\test\\汉字3.txt"));

        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\root\\Desktop\\test\\汉字3.txt"));
    }

1.3 特有的方法

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

范例:

    public static void main(String[] args) throws IOException {
        //readLine
        //创建对象
        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\root\\Desktop\\test\\333.txt"));

        //读取数据
        String line;
        while((line=br.readLine())!=null){
            //把读到的内容输出
            System.out.println(line);
        }
        
        //释放资源
        br.close();

    }
    public static void main(String[] args) throws IOException {
        //newLine
        //创建对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\root\\Desktop\\test\\444.txt"));

        //写入数据
        bw.write("你好啊");
        //写入换号符
        bw.newLine();
        bw.write("我很好");
        //释放资源
        bw.close();
    }
    public static void main(String[] args) throws IOException {
        //newLine
        //创建对象
        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\root\\Desktop\\test\\333.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\root\\Desktop\\test\\444.txt"));

        //循环读写数据  把读到的数据写入目标文件中
        String line;
        while((line=br.readLine())!=null){
            //把读到的数据写入文件
            bw.write(line);
            //写入换行符
            bw.newLine();
        }

        //释放资源
        br.close();
        bw.close();
    }

1.4 应用场景

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

2. 转换流

2.1 概述

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

2.2 分类

流类型
输入流InputStreamReader
输出流OutputStreamWriter

2.2 转换流的使用

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

public InputStreamReader(InputStream in)
public InputStreamReader(InputStream in, String charsetName)
public OutputStreamWriter(OutputStream out)
public OutputStreamWriter(OutputStream out, String charsetName)

范例:

    public static void main(String[] args) throws FileNotFoundException {
        //字节流对象
        FileInputStream fis = new FileInputStream("C:\\Users\\root\\Desktop\\test\\汉字.txt");
        FileOutputStream fos = new FileOutputStream("C:\\Users\\root\\Desktop\\test\\转换流测试.txt");

        //转换成字符流
        InputStreamReader isr = new InputStreamReader(fis);
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        
    }

3. Properties

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

3.1 对象创建

构造方法:

public Properties() 

范例:

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

3.2 常用方法

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

常用方法:

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

范例:

public static void main(String[] args) {
        Properties properties = new Properties();
        //存储key value
        properties.setProperty("name","三更");
        properties.setProperty("age","17");

        //获取对应的值
        String v = properties.getProperty("name");

        Set<String> keys = properties.stringPropertyNames();
        for (String key : keys) {
            String value = properties.getProperty(key);
            System.out.println(key+"===="+value);
        }
    }

3.2 和流结合的方法

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

范例:

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

    }

3.3 应用场景

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值