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

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

Description

read

License

Open Source License

Declaration

static String read(File file, Charset charset) throws IOException 

Method Source Code


//package com.java2s;
/*/*  ww w  .j  ava2s .  com*/
 * This file is part of DeltaEssentials.
 *
 * DeltaEssentials 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.
 *
 * DeltaEssentials 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 DeltaEssentials.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.StandardOpenOption;

public class Main {
    static String read(File file) throws IOException {
        return read(file, StandardCharsets.UTF_8);
    }

    static String read(File file, Charset charset) throws IOException {
        FileLock lock = null;
        FileChannel fileChannel = null;

        try {
            fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE);

            // Lock the file
            lock = fileChannel.lock();

            ByteBuffer buffer = ByteBuffer.allocate((int) fileChannel.size());
            fileChannel.read(buffer);

            return new String(buffer.array(), charset);
        } finally {
            // Release the file lock
            if (lock != null) {
                try {
                    lock.release();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

            // Close the file channel
            if (fileChannel != null) {
                try {
                    fileChannel.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

            // Note: Do NOT return from a finally-block
        }
    }
}

Related

  1. newReader(File file, Charset charset)
  2. newReader(InputStream input, Charset encoding)
  3. newReader(ReadableByteChannel ch, Charset cs)
  4. newUTF8CharSetReader(String filename)
  5. openNewCompressedInputReader(final InputStream inputStream, final Charset charset)
  6. read(InputStream _is, CharsetDecoder _decoder)
  7. read(InputStream in, Charset charset)
  8. read(InputStream in, Charset cs, Appendable appendable, int capacity)
  9. read(InputStream in, String charsetName)