Here you can find the source of readFile(File file, String encoding)
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") |
Parameter | Description |
---|---|
IOException | if there's a problem accessing the file |
private static String readFile(File file, String encoding) throws IOException
//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(); } }