欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

使用BufferedReader读取本地文件的操作

时间:2022-08-24 09:39:58|栏目:JAVA代码|点击:

BufferedReader读取本地文件

在使用BufferedWriter写入文件时,如果忘记关闭文件(close)同时也没有调用flush(),则被写入的文件中是没有内容的。

在关闭文件的同时系统会自动调用flush()

 try {
   File file=new File("c://aa.txt");
   FileInputStream is=new FileInputStream(file);
   
   InputStreamReader reader=new InputStreamReader(is,"GBK");
   BufferedReader br=new BufferedReader(reader);
    String line=null;
   while ((line=br.readLine())!=null) {
    System.out.println(line);
   }
   reader.close();
   br.close();
   is.close();
   
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

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文件:

上一篇:Java常见面试题之final在java中的作用是什么

栏    目:JAVA代码

下一篇:java设计模式之装饰模式详细介绍

本文标题:使用BufferedReader读取本地文件的操作

本文地址:http://www.codeinn.net/misctech/211687.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有