Java Text File Read by Charset readFile(File file, Charset charset)

Here you can find the source of readFile(File file, Charset charset)

Description

read File

License

Apache License

Declaration

public static String readFile(File file, Charset charset) throws IOException 

Method Source Code


//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();
        }
    }
}

Related

  1. reader(Class baseClass, String resourceName, Charset charset)
  2. reader(InputStream in, String charsetName)
  3. readerBuffered(final File file, final Charset charset)
  4. readFile(Bundle bundle, String path, Charset cs)
  5. readFile(File f, Charset chst)
  6. readFile(File file, Charset charset)
  7. readFile(File file, Charset cs)
  8. readFile(File file, String charsetName)
  9. readFile(File theFile, Charset theCharset)