Java Utililty Methods File to Byte Array

List of utility methods to do File to Byte Array

  1. HOME
  2. Java
  3. F
  4. File to Byte Array

Description

The list of methods to do File to Byte Array are organized into topic(s).

Method

byte[] fileToByteArray(File file)
Loads data from file and converts it to a byte array.
byte[] result;
try (FileInputStream fileInputStream = new FileInputStream(file)) {
 result = new byte[(int) file.length()];
 fileInputStream.read(result);
return result;
byte[] fileToByteArray(File file)
Converts a file into an array of bytes.
final byte[] data = new byte[Math.toIntExact(file.length())];
try (FileInputStream fis = new FileInputStream(file)) {
 fis.read(data);
return data;
byte[] FiletoByteArray(File file)
File to ByteArray
try {
 FileInputStream fis = new FileInputStream(file);
 byte[] result = new byte[fis.available()];
 fis.read(result);
 fis.close();
 return result;
} catch (Exception e) {
 e.printStackTrace();
...
byte[] fileToByteArray(File file)
file To Byte Array
byte[] byteArray = new byte[(int) file.length()];
InputStream fis = new FileInputStream(file);
fis.read(byteArray);
fis.close();
return byteArray;
byte[] fileToByteArray(File file)
Reads the contents of a file in to a byte array.
long numOfBytes = file.length();
if (numOfBytes > Integer.MAX_VALUE) {
 throw new IOException("File is to large to be read in to a byte array");
byte[] bytes = new byte[(int) numOfBytes];
FileInputStream ins = new FileInputStream(file);
int offset = 0;
int numRead = 0;
...
byte[] fileToByteArray(final File f)
file To Byte Array
final FileInputStream is = new FileInputStream(f);
final byte[] ret = new byte[(int) f.length()];
int p = 0;
while (p < ret.length)
 p += is.read(ret, p, ret.length - p);
is.close();
return ret;
byte[] fileToByteArray(final IFile file)
file To Byte Array
InputStream contents = null;
try {
 contents = file.getContents();
 return ByteStreams.toByteArray(contents);
} finally {
 if (contents != null)
 contents.close();
byte[] fileToByteArray(String filePath)
Method for converting a file (represented by its path) in an array of bytes
if (COMPILE_FOR_JAVA) {
 File file = new File(filePath);
 byte[] bytes = null;
 try {
 FileInputStream fis = new FileInputStream(file);
 ByteArrayOutputStream bos = new ByteArrayOutputStream();
 byte[] buf = new byte[1024];
 try {
...
byte[] fileToByteArray(String fname)
file To Byte Array
File file = new File(fname);
FileInputStream fis = null;
try {
 fis = new FileInputStream(fname);
} catch (FileNotFoundException e) {
 return null;
byte[] fileContents = new byte[(int) file.length()];
...
byte[] fileToByteArray(String path)
Returns the file at the specified path as a byte array.
File file = new File(path);
if (!file.exists() || file.isDirectory()) {
 return null;
FileInputStream in = null;
try {
 byte[] array = new byte[(int) file.length()];
 in = new FileInputStream(file);
...


AltStyle によって変換されたページ (->オリジナル) /