Code example for FileChannel

Methods: readsizeclose

0
	 
	public static ByteBuffer read(File file) throws Exception {
		FileInputStream fis = new FileInputStream(file);
		FileChannel chan = fis.getChannel();
		 
		long size = chan.size();
		if(size > Integer.MAX_VALUE) {
			throw new IndexOutOfBoundsException("File too large");
		} 
		 
		ByteBuffer buf = ByteBuffer.allocate((int) size);
		 
		chan.read(buf);
		 
		chan.close();
		fis.close();
		 
		buf.flip();
 
		return buf;		
Stop searching for code, let great code find you!  Add Codota to your java IDE