Java File Read via ByteBuffer read(File file)

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

Description

read

License

Open Source License

Declaration

public static String read(File file) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *              OTj/*from  w  ww  .j  a v  a2 s  . com*/
 * Low-level client-side library for Open Transactions in Java
 * 
 * Copyright (C) 2013 by Piotr Kope? (kactech)
 * 
 * EMAIL: pepe.kopec@gmail.com
 * 
 * BITCOIN: 1ESADvST7ubsFce7aEi2B6c6E2tYd4mHQp
 * 
 * OFFICIAL PROJECT PAGE: https://github.com/kactech/OTj
 * 
 * -------------------------------------------------------
 * 
 * LICENSE:
 * This program is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Affero
 * General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * ADDITIONAL PERMISSION under the GNU Affero GPL version 3
 * section 7: If you modify this Program, or
 * any covered work, by linking or combining it with other
 * code, such other code is not for that reason alone subject
 * to any of the requirements of the GNU Affero GPL version 3.
 * (==> This means if you are only using the OTj, then you
 * don't have to open-source your code--only your changes to
 * OTj itself must be open source. Similar to
 * LGPLv3, except it applies to software-as-a-service, not
 * just to distributing binaries.)
 * Anyone using my library is given additional permission
 * to link their software with any BSD-licensed code.
 * 
 * -----------------------------------------------------
 * 
 * You should have received a copy of the GNU Affero General
 * Public License along with this program. If not, see:
 * http://www.gnu.org/licenses/
 * 
 * If you would like to use this software outside of the free
 * software license, please contact Piotr Kope?.
 * 
 * DISCLAIMER:
 * 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 Affero General Public License for
 * more details.
 ******************************************************************************/

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

public class Main {
    public static final String UTF8 = "UTF-8";

    public static String read(String first, String... restOfPath) throws IOException {
        return read(file(first, restOfPath));
    }

    public static String read(File file) throws IOException {
        return new String(readBytes(file), UTF8);
    }

    public static File file(String first, String... restOfPath) {
        File f = new File(first);
        for (String n : restOfPath)
            f = new File(f, n);
        return f;
    }

    public static byte[] readBytes(File f) throws IOException {
        FileInputStream fis = new FileInputStream(f);
        FileChannel fChannel = fis.getChannel();
        byte[] barray = new byte[(int) f.length()];
        ByteBuffer bb = ByteBuffer.wrap(barray);
        //bb.order(ByteOrder.LITTLE_ENDIAN);
        fChannel.read(bb);
        fChannel.close();
        fis.close();
        return bb.array();
    }
}

Related

  1. mapReadWrite(File file)
  2. nioCopy(ReadableByteChannel input, WritableByteChannel output)
  3. openReadOnly(Path path, int offset, int length)
  4. read(DataInput in, int length)
  5. read(File file)
  6. read(File file, long offset, int length)
  7. read(File from)
  8. read(final File source)
  9. read(final FileChannel channel)