- package main.java;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- /**
- * @author YY
- * 读文件
- *
- */
- 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));//FileReader这样new怎么关咧?
- 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));//FileReader这样new怎么关咧?
- String line;
- while ((line = reader.readLine()) != null) {
- // content += line + "\n";
- 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();
- }
- }