BufferedReader 读取文件内容,原样输出

Posted beibidewomen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BufferedReader 读取文件内容,原样输出相关的知识,希望对你有一定的参考价值。

1.新建目标类:

package com.xuzhiwen.io;

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

2.测试类:

package com.xuzhiwen.io;

import java.io.BufferedReader;
import java.io.FileReader;

public class TestBufferedReader {
    public static void main(String[] args) throws Exception {
        String filepath = "src/com/xuzhiwen/io/TargetFile.java";
        BufferedReader read = new BufferedReader(new FileReader(filepath));
        String s;
        StringBuffer sb = new StringBuffer();
        while((s=read.readLine()) != null){
            sb.append(s+"\\n");
        }
        read.close();
        System.out.println(sb);
    }
}

3.输出结果为:

 4.文件结构树:

 

5. 将结果用StringReader再读一次:

package com.xuzhiwen.io;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StringReader;

public class TestBufferedReader {
    public static void main(String[] args) throws Exception {
        String filepath = "src/com/xuzhiwen/io/TargetFile.java";
        BufferedReader read = new BufferedReader(new FileReader(filepath));
        String s;
        StringBuffer sb = new StringBuffer();
        while((s=read.readLine()) != null){
            sb.append(s+"\\n");
        }
        read.close();
        System.out.println(sb);
        
        
        StringReader sr = new StringReader(sb.toString());
        int c;
        while((c=sr.read()) != -1){
            System.out.print((char)c);
        }
    }
}

6. 运行结果如下:

7. 将内容写入文件:

package com.xuzhiwen.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringReader;

public class TestBufferedReader {
    public static void main(String[] args) throws Exception {
        String filepath = "src/com/xuzhiwen/io/TargetFile.java";
        BufferedReader read = new BufferedReader(new FileReader(filepath));
        String s;
        StringBuffer sb = new StringBuffer();
        while((s=read.readLine()) != null){
            sb.append(s+"\\n");
        }
        read.close();
        System.out.println(sb);
        System.out.println("==============================================");
        
        
        StringReader sr = new StringReader(sb.toString());
        int c;
        while((c=sr.read()) != -1){
            System.out.print((char)c);
        }
        sr.close();
        
        System.out.println("==============================================");
        
        String file = "basic.out";
        BufferedReader br = new BufferedReader(new StringReader(sb.toString()));
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
        
        int lineCount = 1;
        String ss;
        while((ss=br.readLine()) != null){
            pw.println(lineCount+":  "+ss);
            lineCount++;
        }
        pw.close();
    }
}

8. 运行结果:

9. 打开: basic.out文件:

 

以上是关于BufferedReader 读取文件内容,原样输出的主要内容,如果未能解决你的问题,请参考以下文章

Java中bufferedReader的readline读取不到文件内容,显示的是null。

在Java中,BufferedReader 是一个啥类啊,有啥作用啊

使用 BufferedReader 读取行并检查文件结尾

java中 BufferedReader类读取文件时

Java 如何读取目录下的文件内容

vb中怎么按某行某列读取本地电脑的“*.txt”文本内容并原样显示到文本框中???