Java File Content to Byte Array getFileContentAsByteArray(String fileName)

Here you can find the source of getFileContentAsByteArray(String fileName)

Description

get File Content As Byte Array

License

LGPL

Declaration

public static byte[] getFileContentAsByteArray(String fileName) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*// w w  w .  j ava  2 s  . com
 * ============================================================================
 * GNU Lesser General Public License
 * ============================================================================
 *
 * ZKTest - Free ZeroKode testing library.
 * Copyright (C) 2011 Telesoft Consulting GmbH http://www.telesoft-consulting.at
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; If not, see http://www.gnu.org/licenses/
 * 
 * Telesoft Consulting GmbH
 * Gumpendorferstra?e 83-85
 * House 1, 1st Floor, Office No.1
 * 1060 Vienna, Austria
 * http://www.telesoft-consulting.at/
 */

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Main {
    public static byte[] getFileContentAsByteArray(String fileName) throws FileNotFoundException, IOException {
        RandomAccessFile raf = new RandomAccessFile(fileName, "r");
        Long lengthFile;
        byte[] b;
        raf.seek(0);
        lengthFile = new Long(raf.length());
        b = new byte[lengthFile.intValue()];
        raf.readFully(b);
        raf.close();
        raf = null;
        lengthFile = null;
        return b;
    }
}

Related

  1. getFileContentAsByteArray(String fileName)
  2. getFileContentAsBytes(String fileName)
  3. getFileContentAsBytes(String fileName)
  4. getFileContentByte(InputStream inputStream, long offset, int length)