Java Byte Array from getBytes(String filePath)

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

Description

get Bytes

License

Apache License

Parameter

Parameter Description

Exception

Parameter Description

Return

: byte[]

Declaration

public static byte[] getBytes(String filePath) 

Method Source Code

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

import java.io.*;

public class Main {

    public static byte[] getBytes(String filePath) {
        byte[] buffer = null;
        try {//from  w  w  w.  ja v a 2  s  . c om
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
}

Related

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