Here you can find the source of getBytes(File file)
Parameter | Description |
---|---|
file | The file |
Parameter | Description |
---|---|
IOException | an exception |
public static byte[] getBytes(File file) throws IOException
//package com.java2s; /**//from w w w . j a v a2 s . c o m * Copyright 2009-2012 Three Crickets LLC. * <p> * The contents of this file are subject to the terms of the LGPL version 3.0: * http://www.gnu.org/copyleft/lesser.html * <p> * Alternatively, you can obtain a royalty free commercial license with less * limitations, transferable or non-transferable, directly from Three Crickets * at http://threecrickets.com/ */ import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class Main { /** * Reads a file into a byte array. * * @param file * The file * @return The byte array * @throws IOException */ public static byte[] getBytes(File file) throws IOException { FileInputStream stream = new FileInputStream(file); try { long length = file.length(); if (length > Integer.MAX_VALUE) throw new IOException("File too big: " + file.getName()); int ilength = (int) length; byte[] bytes = new byte[ilength]; int alength = stream.read(bytes); if (alength != ilength) throw new IOException("Only " + alength + " of " + ilength + " bytes read: " + file.getName()); return bytes; } finally { stream.close(); } } }