Java File Read via ByteBuffer read_file(File input)

Here you can find the source of read_file(File input)

Description

reafile

License

Open Source License

Parameter

Parameter Description
input a parameter

Declaration

public static String read_file(File input) 

Method Source Code

//package com.java2s;
//License from project: Open Source 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 {
    /**/*from ww  w .j a v a 2  s.c  o  m*/
     *
     */
    public static String log;

    /**
     *
     * @param input
     * @return
     */
    public static String read_file(File input) {
        log = "";
        String output = "";

        FileInputStream stream;
        try {
            stream = new FileInputStream(input);
            try {

                FileChannel fc = stream.getChannel();
                MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
                /* Instead of using default, pass in a decoder. */
                output = Charset.defaultCharset().decode(bb).toString();
            } finally {
                stream.close();
            }
        } catch (IOException e) {
            // TODO: handle exception
            log += e.getMessage();
            return "";
        }

        return output;
    }
}

Related

  1. read(InputStream stream)
  2. read(Path file)
  3. read(Path file, int pos, int length)
  4. read(Reader reader)
  5. read24BitInteger(byte[] threeBytes, ByteOrder order)
  6. readAiffHeader(DataInputStream inStrm, FileChannel fc)
  7. readAndTransfer(final String dir)
  8. readAsByteArray(final InputStream source)
  9. readBigEndianWord(byte[] buf)