Java - Write code to read the file content to a string.

Requirements

Write code to read the file content to a string.

Demo

//package com.book2s;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {
    /**/*from w  w  w .  j  a  va 2  s .  c  o  m*/
     * change the content to a string.
     * 
     * @param file
     * @return the content of file.
     */
    public static final String file2String(File file) {
        BufferedReader br;
        StringBuilder strBlder = new StringBuilder("");
        try {
            br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file)));
            String line = "";
            while (null != (line = br.readLine())) {
                strBlder.append(line + "\n");
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return strBlder.toString();
    }
}