集册 Java实例教程 按文件名读取文件并保存到字符串

按文件名读取文件并保存到字符串

欢马劈雪     最近更新时间:2020-01-02 10:19:05

520
按文件名读取文件并保存到字符串
/** nowjava.com - 时代Java 提 供 **/

//package com.nowjava;

import java.io.*;


public class Main {

    public static String readFile(String filename) {

        StringBuilder buf = new StringBuilder();

        try {

            BufferedReader input = new BufferedReader(new FileReader(

                    filename));

            String line;


            while ((line = input.readLine()) != null) {

                buf.append(line);

                buf.append(System.getProperty("line.separator"));

            }

        } 
展开阅读全文