Java IO流读取文本内容

Java IO流读取文本内容

1.使用FileReader,BufferedReader

文本内容
在这里插入图片描述
代码

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Test {

	public static void main(String[] args) {
		FileReader fr = null;
		BufferedReader br = null;
		StringBuffer txt = new StringBuffer();
		try {
			File file = new File("D:\\txt\\Test.txt");
			fr = new FileReader(file);
			br = new BufferedReader(fr);
			String line = null;
			while((line = br.readLine()) != null){
				txt.append(line);
				txt.append("\n");
			}
			br.close();
			fr.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		System.out.println(txt);
	}
}

控制台结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43611145/article/details/86491800
今日推荐