Java Byte Array from getBytes(String fileName)

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

Description

get Bytes

License

Apache License

Declaration

public static byte[] getBytes(String fileName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

public class Main {
    public static byte[] getBytes(String fileName) {
        return getBytes(new File(fileName));
    }/*from  w  w  w  .  j a  v  a2 s . c o  m*/

    public static byte[] getBytes(File file) {
        byte[] outputStr = null;
        BufferedInputStream buffStr = null;
        String fileName = "";
        try {
            fileName = file.getName();
            FileInputStream fileStr = new FileInputStream(file);
            System.out.println("File Stream: " + fileStr.available());
            buffStr = new BufferedInputStream(fileStr);
            System.out.println("Buffered Stream: " + buffStr.available());
            outputStr = new byte[buffStr.available()];
            buffStr.read(outputStr, 0, outputStr.length);
            buffStr.close();
        } catch (Exception ex) {
            System.out.print("Unable to extract bytes from File: " + fileName);
            System.out.println(" : " + ex.getMessage());
            // ex.printStackTrace();
            throw new RuntimeException("Unable to extract bytes from File: " + fileName);
        }
        return outputStr;
    }
}

Related

  1. getBytes(int value)
  2. getBytes(String content)
  3. getBytes(String data, String encoding)
  4. getBytes(String defaultPlain)
  5. getBytes(String fileName)
  6. getBytes(String filePath)
  7. getBytes(String filePath, long startPos, long endPos)
  8. getBytes(String fileUrl)
  9. getBytes(String input)