当前位置: 代码迷 >> 综合 >> (JavaSE 学习记录) IO流之缓冲字符流BufferedReader、BufferedWriter
  详细解决方案

(JavaSE 学习记录) IO流之缓冲字符流BufferedReader、BufferedWriter

热度:6   发布时间:2024-01-09 06:58:59.0

BufferedReader、BufferedWriter

同缓冲字节流一致,利用缓存机制,大大提高了读写文本文件的效率。

BufferedReader新增了逐行读取方法 readLine();
BufferedWriter新增了换行方法 newLine();

import java.io.*;public class BufferedReaderTest {
    public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("Garden.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("Garden-copy.txt"));copy(br, bw);}public static void copy(BufferedReader br, BufferedWriter bw) {
    String str = null;try {
    //读取一整行while ((str = br.readLine()) != null) {
    bw.write(str);bw.newLine();	//换行}bw.flush();} catch (IOException e) {
    e.printStackTrace();} finally {
    try {
    if (null != bw) {
    bw.close();}} catch (IOException e) {
    e.printStackTrace();}try {
    if (null != br) {
    br.close();}} catch (IOException e) {
    e.printStackTrace();}}}
}