JAVA—— BufferedReader、BufferedWriter

缓冲区的出现是为了提高流的操作效率而出现的。
所以在创建缓冲区之前,必须要先有流对象。
该缓冲区中提供了一个跨平台的换行符。
newLine();

import java.io.*;
class  BufferedWriterDemo
{
    public static void main(String[] args) throws IOException
    {
        //创建一个字符写入流对象。
        FileWriter fw = new FileWriter("buf.txt");
        //为了提高字符写入流效率。加入了缓冲技术。
        //只要将需要被提高效率的流对象作为参数传递给缓冲区的构造函数即可。
        BufferedWriter bufw = new BufferedWriter(fw);
        for(int x=1; x<5; x++)
        {
            bufw.write("abcd"+x);
            bufw.newLine();
            bufw.flush();
        }
        //记住,只要用到缓冲区,就要记得刷新。
        //bufw.flush();
        //其实关闭缓冲区,就是在关闭缓冲区中的流对象。
        bufw.close();
    }
}

结果为:

abcd1
abcd2
abcd3
abcd4

字符读取流缓冲区:
该缓冲区提供了一个一次读一行的方法 readLine,方便于对文本数据的获取。
当返回null时,表示读到文件末尾。
readLine方法返回的时候只返回回车符之前的数据内容。并不返回回车符。

import java.io.*;
class  BufferedReaderDemo
{
    public static void main(String[] args) throws IOException
    {
        //创建一个读取流对象和文件相关联。
        FileReader fr = new FileReader("buf.txt");
        //为了提高效率。加入缓冲技术。将字符读取流对象作为参数传递给缓冲对象的构造函数。
        BufferedReader bufr = new BufferedReader(fr);
        String line = null;
        while((line=bufr.readLine())!=null)
        {
            System.out.println(line);
        }
        bufr.close();
    }
}

通过缓冲区复制一个.java文件。

import java.io.*;
class CopyTextByBuf
{
    public static void main(String[] args)
    {
        BufferedReader bufr = null;
        BufferedWriter bufw = null;
        try
        {
            bufr = new BufferedReader(new FileReader("BufferedWriterDemo.java"));
            bufw = new BufferedWriter(new FileWriter("demo.txt"));
            String line = null;
            while((line = bufr.readLine())!=null)
            {
                bufw.write(line);
                bufw.newLine();
                bufw.flush();
            }
        }
        catch (IOException e)
        {
            throw new RuntimeException("读写失败");
        }
        finally
        {
            try
            {
                if(bufr!=null)
                    bufr.close();
            }
            catch (IOException e)
            {
                throw new RuntimeException("读取关闭失败");
            }
            try
            {
                if(bufw!=null)
                    bufw.close();
            }
            catch (IOException e)
            {
                throw new RuntimeException("写入关闭失败");
            }
        }
    }
}

明白了BufferedReader类中特有方法readLine的原理后,
可以自定义一个类中包含一个功能和readLine一致的方法。
来模拟一下BufferedReader

import java.io.*;
class MyBufferedReader
{
    //这里用到了装饰模式
    private FileReader r;
    MyBufferedReader(FileReader r)
    {
        this.r = r;
    }
    //可以一次读一行数据的方法。
    public String myReadLine()throws IOException
    {
        //定义一个临时容器。原BufferReader封装的是字符数组。
        //为了演示方便。定义一个StringBuilder容器。因为最终还是要将数据变成字符串。
        StringBuilder sb = new StringBuilder();
        int ch = 0;
        while((ch = r.read())!=-1)
        {
            if(ch =='\r')
                continue;
            if(ch=='\n')
                return sb.toString();
            else
                sb.append((char)ch);
        }
        if(sb.length()!=0)
            return sb.toString();
        return null;
    }
    public void myclose()throws IOException
    {
        r.close();
    }
}

class  MyBufferedReaderDemo
{
    public static void main(String[] args)throws IOException
    {
        FileReader fr = new FileReader("demo.txt");
        MyBufferedReader mybuf = new MyBufferedReader(fr);
        String line = null;
        while((line =mybuf.myReadLine())!=null)
        {
            System.out.println(line);
        }
        mybuf.myclose();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以按照以下步骤使用Java来修改txt文件中某一行的内容: 1.使用BufferedReader读取文件内容并保存到List中 ``` List<String> lines = new ArrayList<>(); BufferedReader reader = new BufferedReader(new FileReader("file.txt")); String line = reader.readLine(); while (line != null) { lines.add(line); line = reader.readLine(); } reader.close(); ``` 2.修改List中的某一行内容 ``` lines.set(index, newLine); ``` 其中,index表示要修改的行的索引,newLine表示修改后的内容。 3.使用BufferedWriter将修改后的内容写回文件中 ``` BufferedWriter writer = new BufferedWriter(new FileWriter("file.txt")); for (String newline : lines) { writer.write(newline); writer.newLine(); } writer.close(); ``` 完整代码示例: ``` import java.io.*; import java.util.*; public class ModifyFile { public static void main(String[] args) throws IOException { List<String> lines = new ArrayList<>(); BufferedReader reader = new BufferedReader(new FileReader("file.txt")); String line = reader.readLine(); while (line != null) { lines.add(line); line = reader.readLine(); } reader.close(); int index = 2; //要修改的行的索引 String newLine = "new content"; //修改后的内容 lines.set(index, newLine); BufferedWriter writer = new BufferedWriter(new FileWriter("file.txt")); for (String newline : lines) { writer.write(newline); writer.newLine(); } writer.close(); } } ``` 注意:如果文件过大,将整个文件读入内存可能会导致内存溢出,需要使用更高效的方式处理文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值