Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 0.47 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I use ReadableByteChannel to get file contents and store it in byteBuffer?
  2. File file = new File(fileName);
  3. try {
  4.     ReadableByteChannel channel = new FileInputStream(fileName).getChannel();
  5.     ByteBuffer buf = ByteBuffer.allocateDirect(file.length());
  6.  
  7.     // How can use buf.read to get all the contents?
  8.  
  9. } catch (Exception e){
  10.  
  11. }
  12.        
  13. FileChannel channel = new FileInputStream(fileName).getChannel();
  14. ByteBuffer buf = channel.map(MapMode.READ_ONLY,0,channel.size());