IO流之BufferedReader和BufferedWriter

IO流之BufferedReader和BufferedWriter

BufferedReader

现成reader

以下代码使用BufferedReader来读取硬盘文本文件:

package com.javalearn.io.buffer;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TestForBufferedReader {
    public static void main(String[] args) {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("D:\\typora笔记\\java\\io流\\临时文件夹\\新.java");
            BufferedReader reader = new BufferedReader(fileReader);  // fileReader为节点流,reader为包装流/处理流
            String s = " ";
            while((s=reader.readLine())!=null) { // readLine读到末尾的时候返回null
                System.out.println(s);  // readLine不会读出换行符,因此需要打印时换行
            }
            if (reader != null) {
                try {
                    reader.close();  // 包装流的close包含了节点流的close
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

输出结果:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}

构造reader

上文中,FileReader对象作为参数传入BufferedReader的构造函数中,实际上可以把FileInputStream转换为Reader,再传入BufferedReader的构造函数中:

package com.javalearn.io.change;

import java.io.*;

public class TestForMultiSon {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\typora笔记\\java\\io流\\临时文件夹\\新.java")));  // 节点流和包装流是相对关系。字节流转为字符流,作为参数送入BufferedReader的构造函数。
        String line = null;
        while ((line = br.readLine())!=null) {
            System.out.println(line);
        }
        br.close();
    }
}

输出结果:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}

BufferedWriter

现成writer

以下代码使用BufferedWriter往硬盘写数据:

package com.javalearn.io.bufferedwriter;

import java.io.BufferedWriter;
import java.io.FileWriter;

public class TestForBufferedWriter {
    public static void main(String[] args) throws Exception{
        BufferedWriter writer = new BufferedWriter(new FileWriter("file1"));
        writer.write("hello world\nhello kitty");
        writer.flush();
        writer.close();
    }
}

输出文件内容:

hello world
hello kitty

构造writer

以下代码也是往硬盘写数据,不过构造函数中的writer是间接构造出来的:

package com.javalearn.io.bufferedwriter;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;

public class TestForBufferedWriter {
    public static void main(String[] args) throws Exception{
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("file1")));
        writer.write("我现在很饿");  // 往硬盘写
        writer.flush();
        writer.close();
    }
}

输出文件内容:

我现在很饿
上一篇:JAVA IO -文本文件复制(使用字符处理流实现)- 可直接使用


下一篇:Java第十五章课后小结