Escribir byte [] al archivo en Java [cerrado]


¿Cómo convertir una matriz de bytes a un archivo en Java?

byte[] objFileBytes, File objFile
Author: cricket_007, 2011-07-26

5 answers

Un objeto File no contiene el contenido del archivo. Es solo un puntero al archivo en su disco duro (u otro medio de almacenamiento, como un SSD, unidad USB, recurso compartido de red). Así que creo que lo que quieres es escribirlo en el disco duro.

Tienes que escribir el archivo usando algunas clases en la API de Java

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(yourFile));
bos.write(fileBytes);
bos.flush();
bos.close();

También puedes usar un Writer en lugar de un OutputStream. El uso de un escritor le permitirá escribir texto (String, char []).

BufferedWriter bw = new BufferedWriter(new FileWriter(yourFile));

Ya que dijiste que querías para guardar todo en la memoria y no querer escribir nada, puede intentar usar ByteArrayInputStream. Esto simula un flujo de entrada, que puede pasar a la mayoría de las clases.

ByteArrayInputStream bais = new ByteArrayInputStream(yourBytes);
 65
Author: Martijn Courteaux,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-25 22:05:03
public void writeToFile(byte[] data, String fileName) throws IOException{
  FileOutputStream out = new FileOutputStream(fileName);
  out.write(data);
  out.close();
}
 22
Author: Caner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-07 12:00:26

Utilice un FileOutputStream.

FileOutputStream fos = new FileOutputStream(objFile);
fos.write(objFileBytes);
fos.close();
 4
Author: perp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-26 10:31:42

Ok, tú lo pediste:

File file = new File("myfile.txt");

// convert File to byte[]
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(file);
bos.close();
oos.close();
byte[] bytes = bos.toByteArray();

// convert byte[] to File
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
File fileFromBytes = (File) ois.readObject();
bis.close();
ois.close();

System.out.println(fileFromBytes);

Pero esto no tiene sentido. Por favor, especifique lo que está tratando de lograr.

 4
Author: Adriaan Koster,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-26 11:29:31

Vea Cómo renderizar PDF en Android. Parece que no tiene ninguna opción excepto guardar el contenido en un (temporal) en el archivo SD para poder mostrarlo en el visor de pdf.

 1
Author: pap,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:02:45