Here you can find the source of getBytesFromFile(File file)
Parameter | Description |
---|---|
file | the file from which the bytes should be extracted from |
Parameter | Description |
---|---|
IOException | an exception |
public static byte[] getBytesFromFile(File file) throws IOException
//package com.java2s; /*//ww w.j a v a2 s .c o m * Created on 14-Jan-2004 at 16:07:22. * * Copyright (c) 2004-2005 Robert Virkus / Enough Software * * This file is part of J2ME Polish. * * J2ME Polish is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * J2ME Polish is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with J2ME Polish; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Commercial licenses are also available, please * refer to the accompanying LICENSE.txt or visit * http://www.j2mepolish.org for details. */ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class Main { /** * Extracts the bytes from a file. * @param file the file from which the bytes should be extracted from * @return a byte arry corresponding to the file. Is never null. * @throws IOException */ public static byte[] getBytesFromFile(File file) throws IOException { FileInputStream inputStream = null; ByteArrayOutputStream outputStream = null; try { byte[] buffer = new byte[4096]; inputStream = new FileInputStream(file); outputStream = new ByteArrayOutputStream(); int read; while ((read = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, read); } byte[] byteArray = null; byteArray = outputStream.toByteArray(); return byteArray; } finally { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } } } }