Here you can find the source of readFile(File file, Charset charset)
public static String readFile(File file, Charset charset) throws IOException
//package com.java2s; /* //from w ww.jav a 2s . co m * Copyright 2003,2004 Peter Lee, Colin Crist * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ 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 { public static String readFile(File file) throws IOException { return readFile(file, Charset.defaultCharset()); } public static String readFile(File file, Charset charset) throws IOException { FileInputStream stream = new FileInputStream(file); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return charset.decode(bb).toString(); } finally { stream.close(); } } }