Java File Read via ByteBuffer readFileToBuffer(java.io.File file)

Here you can find the source of readFileToBuffer(java.io.File file)

Description

read File To Buffer

License

Open Source License

Declaration

private static java.nio.ByteBuffer readFileToBuffer(java.io.File file)
            throws IOException 

Method Source Code

//package com.java2s;
import java.io.*;

import java.nio.channels.FileChannel;

public class Main {
    private static java.nio.ByteBuffer readFileToBuffer(java.io.File file)
            throws IOException {
        FileInputStream is = new FileInputStream(file);
        try {//from  w  w w .jav a 2 s .  co  m
            FileChannel fc = is.getChannel();
            java.nio.ByteBuffer buffer = java.nio.ByteBuffer
                    .allocate((int) fc.size());
            for (int count = 0; count >= 0 && buffer.hasRemaining();) {
                count = fc.read(buffer);
            }
            buffer.flip();
            return buffer;
        } finally {
            is.close();
        }
    }
}

Related

  1. readFileFragment(FileChannel fc, long pos, int size)
  2. readFileHeader(FileInputStream fpi)
  3. readFileIntoString(File localFile)
  4. readFileIntoString(String path)
  5. readFileNIO(String path, StringBuilder builder)
  6. readFileToString(String path)
  7. readFileToString(String path)
  8. readFlashString(DataInputStream s)
  9. readFloat(BufferedReader br)