Here you can find the source of loadContents(File file)
Loads a file contents as a String using platform encoding.
Parameter | Description |
---|---|
file | file to load. |
Parameter | Description |
---|---|
Exception | an exception |
public static String loadContents(File file) throws IOException
//package com.java2s; /* ********************************************************************* * This file is part of Hannah./*from www. jav a 2 s. c o m*/ * * Hannah is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Hannah 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Hannah. If not, see <http://www.gnu.org/licenses/>. * ******************************************************************* */ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class Main { /** * <p>Loads a file contents as a String using platform encoding.</p> * @param file file to load. * @return the file contents as a {@link String}. * @throws Exception */ public static String loadContents(File file) throws IOException { final StringBuilder contents = new StringBuilder(); final BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); try { final byte[] buffer = new byte[1024]; int read = in.read(buffer); while (read >= 0) { contents.append(new String(buffer, 0, read)); read = in.read(buffer); } } finally { in.close(); } return contents.toString(); } }