How to convert an .exe file to an array of bytes?

0

How can I convert a file with an .exe extension to an array of bytes using java? I have tried with the following code, but it only works for text files.

File soft = jcf.getSelectedFile();

FileInputStream fis = new FileInputStream(soft);
archivoBytes = new byte[(int) soft.length()];
BufferedInputStream bis = new BufferedInputStream(fis);
int leidos;

while ((leidos = bis.read(b)) > 0) {
      bis.read(archivoBytes, 0, leidos);
}
bis.close();

What am I doing wrong? Is there any other method for conversion?

    
asked by Ken 17.10.2016 в 07:09
source

2 answers

1

For this specific case, an instance of BufferedInputStream is not required nor is the cycle while , but simply:

File soft = new File("C:\Windows\notepad.exe");
byte[] archivoBytes = new byte[(int) soft.length()];
FileInputStream fis = new FileInputStream(soft);
fis.read(archivoBytes);
fis.close();
    
answered by 17.10.2016 / 15:46
source
0

Another way would be through the readAllBytes of class File

  public static void main(String[] args) throws IOException  {
    Path archivo= Paths.get("urldelexe");
    byte[] data = Files.readAllBytes(archivo);
     /* Impresión de los Bytes Obtenidos*/
    for (byte e : data) {
        System.out.println(e);
    }
  }

 /* Una Sola Línea */
 byte[] array = Files.readAllBytes(new File("urldelexe").toPath());
    
answered by 17.10.2016 в 07:26