Java InputStreamReader Read load(File file, String encoding)

Here you can find the source of load(File file, String encoding)

Description

Read a text file into a string.

License

Open Source License

Parameter

Parameter Description
file - the text file
encoding - text file content's encoding

Exception

Parameter Description
IOException an exception

Return

the file content as string

Declaration

public static String load(File file, String encoding) throws IOException 

Method Source Code


//package com.java2s;
/*/*w ww  .j  a  v a  2s  . c  o m*/
 * This file is part of Pustefix.
 *
 * Pustefix is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Pustefix is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Pustefix; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

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

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {
    /**
     * Read a text file into a string.
     * 
     * @param file - the text file
     * @param encoding - text file content's encoding
     * @return the file content as string
     * @throws IOException
     */
    public static String load(File file, String encoding) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(fis, encoding);
        StringBuffer strBuf = new StringBuffer();
        char[] buffer = new char[4096];
        int i = 0;
        try {
            while ((i = reader.read(buffer)) != -1)
                strBuf.append(buffer, 0, i);
        } finally {
            fis.close();
        }
        return strBuf.toString();
    }
}

Related

  1. load(final File file, final Class clazz)
  2. load(String fileName)
  3. loadAsText(InputStream in, String encoding, int bufferSize)
  4. readFile(Class cl, String filename)