码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-08-11 16:03:15      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:basic   package   reader   读取文件   结构   pen   buffere   end   技术分享   

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 读取文件内容,原样输出

标签:basic   package   reader   读取文件   结构   pen   buffere   end   技术分享   

原文地址:http://www.cnblogs.com/beibidewomen/p/7345974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!