Java File to Byte Array getBytesFromFile(File f)

Here you can find the source of getBytesFromFile(File f)

Description

get Bytes From File

License

Open Source License

Declaration

private static byte[] getBytesFromFile(File f) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2010 BEA Systems, Inc, IBM Corporation, and others
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*ww w .java  2  s . c  om*/
 *    mkaufman@bea.com - initial API and implementation
 *    
 *******************************************************************************/

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

public class Main {
    private static byte[] getBytesFromFile(File f) throws IOException {
        FileInputStream fis = null;
        ByteArrayOutputStream baos = null;
        byte[] rtrn = new byte[0];
        try {
            fis = new FileInputStream(f);
            baos = new ByteArrayOutputStream();
            int b;
            while ((b = fis.read()) != -1)
                baos.write(b);
            rtrn = baos.toByteArray();
        } finally {
            if (fis != null)
                fis.close();
            if (baos != null)
                baos.close();
        }
        return rtrn;

    }
}

Related

  1. getBytes(File file)
  2. getBytes(File file)
  3. getBytes(File file)
  4. getBytes(File file)
  5. getBytes(File file)
  6. getBytesFromFile(File file)
  7. getBytesFromFile(File file)
  8. getBytesFromFile(File file)
  9. getBytesFromFile(File file)