Here you can find the source of readFile(File f)
Parameter | Description |
---|---|
f | The text file to read |
Parameter | Description |
---|---|
IOException | an exception |
public static String readFile(File f) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2012 BragiSoft, Inc.//from ww w. j av a 2 s. c o m * This source is subject to the BragiSoft Permissive License. * Please see the License.txt file for more information. * All other rights reserved. * * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A * PARTICULAR PURPOSE. * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * Contributors: * Jan-Christoph Klie - Everything * *******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; public class Main { /** * Reads the contents of a text file and returns them in a string * @param f The text file to read * @return The contents of the file as a string * @throws IOException */ public static String readFile(File f) throws IOException { FileInputStream stream = new FileInputStream(f); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } } }