Java File Read via ByteBuffer readFile(File file)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(File file) throws IOException 

Method Source Code


//package com.java2s;
/*/*ww w . j av  a  2  s  .  c  o m*/
 * Dynamic Compressor - Java Library
 * Copyright (c) 2011-2012, IntelligentCode ZhangLixin.
 * All rights reserved.
 * intelligentcodemail@gmail.com
 *
 * GUN GPL 3.0 License
 *
 * http://www.gnu.org/licenses/gpl.html
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class Main {

    public static String readFile(File file) throws IOException {
        if (!file.exists() || file.isDirectory() || !file.canRead()) {
            return null;
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        return readFile(fileInputStream);
    }

    public static String readFile(FileInputStream fileInputStream) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        StringBuffer contentBuffer = new StringBuffer();
        Charset charset = null;
        CharsetDecoder decoder = null;
        CharBuffer charBuffer = null;
        try {
            FileChannel channel = fileInputStream.getChannel();
            while (true) {
                buffer.clear();
                int pos = channel.read(buffer);
                if (pos == -1) {
                    break;
                }
                buffer.flip();
                charset = Charset.forName("UTF-8");
                decoder = charset.newDecoder();
                charBuffer = decoder.decode(buffer);
                contentBuffer.append(charBuffer.toString());
            }
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return contentBuffer.toString();
    }
}

Related

  1. readDouble(FileChannel fileChannel, ByteOrder byteOrder)
  2. readDouble(final byte[] bytes, final int length, final int offset)
  3. readEntireFile(java.io.File file)
  4. readerToString(Reader reader)
  5. readFC(String fname, int length)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)