Java InputStreamReader Read readFile(File file, String encoding)

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

Description

Helper method to read the file's content into a String

License

Open Source License

Parameter

Parameter Description
file the file to be read
encoding the encoding to be used to read the file (must be a valid encoding like "UTF-8")

Exception

Parameter Description
IOException if there's a problem accessing the file

Return

String containing the file's content

Declaration

private static String readFile(File file, String encoding)
        throws IOException 

Method Source Code

//package com.java2s;
/**/*  ww w .j  a v a2  s.c  o m*/
 * Copyright (c) 2014 mediaworx berlin AG (http://mediaworx.com)
 *
 * This library 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 3 of the License, or (at your option) any later version.
 *
 * This library 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.
 *
 * For further information about mediaworx berlin AG, please see the
 * company website: http://mediaworx.com
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 * If not, see <http://www.gnu.org/licenses/>
 */

import java.io.*;

public class Main {
    /**
     * Helper method to read the file's content into a String
     * @param file the file to be read
     * @param encoding  the encoding to be used to read the file (must be a valid encoding like "UTF-8")
     * @return String containing the file's content
     * @throws IOException if there's a problem accessing the file
     */
    private static String readFile(File file, String encoding)
            throws IOException {
        InputStreamReader in = new InputStreamReader(new FileInputStream(
                file), encoding);
        BufferedReader reader = new BufferedReader(in);
        StringBuilder fileContent = new StringBuilder();
        String line = reader.readLine();
        while (line != null) {
            fileContent.append(line).append('\n');
            line = reader.readLine();
        }
        return fileContent.toString();
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file, String default_encoding)
  4. readFile(File file, String encoding)
  5. readFile(File file, String encoding)
  6. readFile(File file, String encoding)
  7. readFile(File filename)
  8. readFile(File path)
  9. readFile(File resultFile)