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

网友投稿 973 2022-12-26

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

使用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+"\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;

http:// while((ss=br.readLine()) != null){

pw.println(lineCount+": "+ss);

lineCount++;

}

DdUjg pw.close();

}

}

8. 运行结果:

9. 打开: basic.out文件:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序和app有什么区别(微信小程序与app的区别)
下一篇:IDEA整合jeesite4.x及安装教程
相关文章

 发表评论

暂时没有评论,来抢沙发吧~