Java File Read via ByteBuffer readFile(String name)

Here you can find the source of readFile(String name)

Description

read File

License

Open Source License

Declaration

public static byte[] readFile(String name) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    public static byte[] readFile(String name) {
        try {/* ww w. j ava2s .co  m*/
            RandomAccessFile raf = new RandomAccessFile(name, "r");
            ByteBuffer buf = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, raf.length());
            try {
                if (buf.hasArray()) {
                    return buf.array();
                } else {
                    byte[] array = new byte[buf.remaining()];
                    buf.get(array);
                    return array;
                }
            } finally {
                raf.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. readFile(String filename)
  2. readFile(String fileName)
  3. readFile(String filename)
  4. readFile(String filePath)
  5. readFile(String name)
  6. readFile(String path)
  7. readFile(String path)
  8. readFile(String path)
  9. readFile(String path)