33

How to convert byte array to File in Java?

byte[] objFileBytes, File objFile
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
farajnew
  • 926
  • 2
  • 9
  • 15
  • 1
    "Convert array of bytes to File object" does not make sense in any other way than that you want to write the bytes to a file. You have at least 3 different answers below on how to do that. Is this what you are after? Otherwise, please clarify. – pap Jul 26 '11 at 11:14
  • 1
    I am developing in android ... I have a file saved in disk encrypted and unlock the encryption so i got the array of bytes so i want to convert them to file to show the pdf file in pdf viewer but without saving the file in disk without encryption – farajnew Jul 26 '11 at 11:21
  • 1
    Well, that's a completely different thing then. You want to launch a pdf-viewer from, essentially, something like a MemoryFile (http://developer.android.com/reference/android/os/MemoryFile.html). Not sure how you would do that, or how you display the pdf. Do you have some code or information you can add on how you actually call the pdf viewer? And which viewer you are using? – pap Jul 26 '11 at 12:17
  • Please change the question and title to reflect the fact, that you are asking about something related to PDF. Question looks confusing for now. – Mike Keskinov Aug 29 '12 at 16:22
  • 2
    -1 The question is completely unrelated to what you want to know. Create a new question and ask "How can I render PDF on Android from bytes in memory?" – Aaron Digulla Oct 10 '12 at 13:25

5 Answers5

90

A File object doesn't contain the content of the file. It is only a pointer to the file on your hard drive (or other storage medium, like an SSD, USB drive, network share). So I think what you want is writing it to the hard drive.

You have to write the file using some classes in the Java API

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

You can also use a Writer instead of an OutputStream. Using a writer will allow you to write text (String, char[]).

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

Since you said you wanted to keep everything in memory and don't want to write anything, you might try to use ByteArrayInputStream. This simulates an InputStream, which you can pass to the most of the classes.

ByteArrayInputStream bais = new ByteArrayInputStream(yourBytes);
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • The file object is null ? and i dont want anything to be saved in Disk ... Its ok to be in memory,So How to Convert array of bytes to File object – farajnew Jul 26 '11 at 11:10
  • 1
    I got this java.io.IOException: open failed: EACCES (Permission denied) in android application while writing BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(yourFile)); – Sanket Thakkar Aug 02 '13 at 07:41
  • @SanketThakkar: You probably don't have the write access (permission) to write a file on that location. Try a different location. – Martijn Courteaux Aug 02 '13 at 22:32
27
public void writeToFile(byte[] data, String fileName) throws IOException{
  FileOutputStream out = new FileOutputStream(fileName);
  out.write(data);
  out.close();
}
Caner
  • 57,267
  • 35
  • 174
  • 180
  • The file object is null ? and i dont want anything to be saved in Disk ... Its ok to be in memory,So How to Convert array of bytes to File object – farajnew Jul 26 '11 at 11:09
  • 2
    File is just an abstract representation of file and directory pathnames. It doesn't contain the content of the file. And a `file` by definition cannot be in memory, so you cannot do what you want. Why do you want to that anyways, it doesn't make much sense? If you explain that maybe people can give other suggestions. – Caner Jul 26 '11 at 11:15
  • I am developing in android ... I have a file saved in disk encrypted and unlock the encryption so i got the array of bytes so i want to convert them to file to show the pdf file in pdf viewer but without saving the file in disk without encryption – farajnew Jul 26 '11 at 11:20
  • That's something completely different. I don't know if that is possible. – Caner Jul 26 '11 at 12:34
5

Use a FileOutputStream.

FileOutputStream fos = new FileOutputStream(objFile);
fos.write(objFileBytes);
fos.close();
perp
  • 3,883
  • 4
  • 31
  • 29
4

Ok, you asked for it:

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);

But this is pointless. Please specify what you are trying to achieve.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
  • He explained what he actually wants, pls check comments – Caner Jul 26 '11 at 11:31
  • 6
    Tnx, after reading that I concluded he is too clueless to help. With this I mean he needs to be at least aware of the problem he is trying to solve and provide the relevant information, which is not the case on both counts. – Adriaan Koster Jul 26 '11 at 13:58
  • I am sad about the down votes since I am one of the few here who answered the question as it was asked. – Adriaan Koster Jul 01 '15 at 06:57
1

See How to render PDF in Android. It looks like you may not have any option except saving the content to a (temporary) on the SD file in order to be able to display it in the pdf viewer.

Community
  • 1
  • 1
pap
  • 27,064
  • 6
  • 41
  • 46
  • The question have nothing to do with PDF – Mike Keskinov Aug 28 '12 at 13:25
  • @MikeKeskinov read the comment from the questioner. It has everything to do with PDF. – pap Aug 29 '12 at 06:27
  • 2
    Then I'm down-voting question. I found this question when trying find info how save byte[] to file. The question itself doesn't mention PDF. Need to change question and title otherwise it is confusing. – Mike Keskinov Aug 29 '12 at 16:21