Conversão de um ficheiro para um byte[]
Página 1 de 1
Conversão de um ficheiro para um byte[]
- Código:
// Obtenção dos dados de um ficheiro num Array de bytes
private static byte[] fileToByteArray(File fFile) throws IOException {
InputStream is = new FileInputStream(fFile);
// Obtenção do tamanho do ficheiro
long length = fFile.length();
// Temos de verificar que o ficheiro não é demasiado grande (não maior do que o máximo valor de um inteiro.
if (length > Integer.MAX_VALUE) {
System.out.println("File is too large...");
return null;
}
byte[] bytes = new byte[(int)length];
// Leitura dos dados para o array de bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Verifica que o ficheiro foi todo percorrido
if (offset < bytes.length) {
throw new IOException("Error");
}
// Close the input stream and return bytes
is.close();
return bytes;
}
Tópicos semelhantes
» Conversão de um byte[] para um ficheiro
» Conversão de um Ficheiro para um URL
» Conversão de String para Date
» Conversão de uma string para um inteiro
» Escrever para um ficheiro
» Conversão de um Ficheiro para um URL
» Conversão de String para Date
» Conversão de uma string para um inteiro
» Escrever para um ficheiro
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos
|
|
Ter Set 24, 2019 1:16 pm por jpmartins
» Artigo Bitcoin
Ter Set 24, 2019 1:13 pm por jpmartins
» Novo site para desportistas / New site for athletes
Ter Dez 04, 2018 8:19 am por Admin
» Check used port with the command line
Ter Dez 04, 2018 8:10 am por Admin
» Check the port where mariaDB is running
Seg Dez 03, 2018 4:59 am por Admin
» Check the linux version
Seg Dez 03, 2018 3:12 am por Admin
» Granting rights to a database for a user
Qui Nov 29, 2018 2:04 am por Admin
» Create new user
Qui Nov 29, 2018 2:01 am por Admin
» Change level of password security level
Qui Nov 29, 2018 1:59 am por Admin