read File by file name and save to String - Java File Path IO

Java examples for File Path IO:Text File

Description

read File by file name and save to String

Demo Code


//package com.java2s;
import java.io.*;

public class Main {
    public static String readFile(String filename) {
        StringBuilder buf = new StringBuilder();
        try {//from  w w w  .  j a  v a2s . co  m
            BufferedReader input = new BufferedReader(new FileReader(
                    filename));
            String line;

            while ((line = input.readLine()) != null) {
                buf.append(line);
                buf.append(System.getProperty("line.separator"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buf.toString();
    }
}

Related Tutorials