服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 使用BufferedReader读取本地文件的操作

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

2021-10-18 13:22qq_42772927 Java教程

这篇文章主要介绍了使用BufferedReader读取本地文件的操作,具有很好的参考价值,希望对大家有所帮助。

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+"
");
        }
        read.close();
        System.out.println(sb);
    }
}

3.输出结果为:

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

4.文件结构树:

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

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+"
");
        }
        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. 运行结果如下:

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

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+"
");
        }
        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. 运行结果:

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

9. 打开: basic.out文件:

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_42772927/article/details/91489234

延伸 · 阅读

精彩推荐