Here you can find the source of readToByteArray(String fname)
Parameter | Description |
---|---|
fname | the file name |
Parameter | Description |
---|---|
IOException | in case of error. |
public static byte[] readToByteArray(String fname) throws IOException
//package com.java2s; import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main { /**/*from w w w.j ava 2 s . c om*/ * read a file into a byte array. * @param fname the file name * @return byte[] a byte array with file contents. * @throws IOException in case of error. */ public static byte[] readToByteArray(String fname) throws IOException { File f = new File(fname); long l = f.length(); FileInputStream fs = new FileInputStream(fname); FileChannel fc = fs.getChannel(); ByteBuffer dst = ByteBuffer.allocate((int) l); fc.read(dst); byte b[] = dst.array(); fc.close(); fs.close(); return (b); } }