文件读写加缓存
原创
©著作权归作者所有:来自51CTO博客作者lihuisssss的原创作品,请联系作者获取转载授权,否则将追究法律责任
- package main.java;
-
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
-
-
-
-
-
-
- public class ReadFile {
- public static void main(String[] args) {
- String path = "D:/中国人才热线.txt";
- String content = "";
- BufferedReader reader = null;
-
- try {
- reader = new BufferedReader(new FileReader(path));
-
- String line;
- while ((line = reader.readLine()) != null) {
- content += line + "\n";
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- System.out.println(content);
- }
-
- public static String getFileContent (String filePath) {
- StringBuffer content = new StringBuffer();
-
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader(filePath));
- String line;
- while ((line = reader.readLine()) != null) {
-
- content.append(line);
- content.append("\n");
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return content.toString();
- }
-
- }