I'm using an API that requires a java.io.File as input, but I'd like to just send the API a byte array. Is there a way to create a File object that will read from my byte array rather than from disk?
-
1possible duplicate of [Creating a File from byte array](http://stackoverflow.com/questions/3704554/creating-a-file-from-byte-array) – Joachim Sauer Jun 11 '12 at 14:49
-
@JoachimSauer not really, as the linked questions asks if it is possible to write to a File without writing to disk (some sort of virtual file or so), whereas this questions simply asks how to write to a File. – brimborium Jun 11 '12 at 14:56
-
@brimborium: the problems looks pretty much the same to me: They *need* a `java.io.File` because some third-party API demands it, but they *have* a byte-array/stream and don't want to persist it to disk (which would be the straight-forward but inefficient solution). Also, the answer is the same: no, it can't be done. – Joachim Sauer Jun 11 '12 at 14:57
-
@JoachimSauer Ok, I see your point. – brimborium Jun 11 '12 at 14:59
4 Answers
A File
does not read anything. It represents a file (or directory) object in the Operating System (or virtual file, etc.). To read from it, you wil have to create Readers or Streams, which the API will do on its own. If the API does not provide methods to pass in a Reader or InputStream, you are out of luck.

- 28,265
- 3
- 46
- 55
You could open a temporary file: open temp file in java
Then if take your byte[] and write it all to the temporary file through some OutputStream/Writer you should have what you need. Then you can pass the file without creating it on disk.
Java works with InputStream
(or OutpusStream
) approach. Using a java.io.File
already enforces too much, then a File
class is not anything on which you can read or write: it just represents an entry in a file system (from which you can then, but just then, obtain its associated input and output streams).
So you should have a method which accepts a generic InputStream
. If the API just supports a File
object then it's clearly poorly designed (or you are trying to use them in a non allowed way).

- 131,802
- 30
- 241
- 343
String strFilePath = "filePath";
byte[] writeBytes = ...;
try {
FileOutputStream fos = new FileOutputStream(strFilePath);
fos.write(byteArray);
fos.close();
} catch(FileNotFoundException ex) {
// handle FileNotFoundException here
} catch(IOException ioe) {
// handle IOException here
}
EDIT: Usually I am not a fan of just giving code, but this whole streaming stuff used to confuse me quite a bit, so here is a solution ;).
EDIT2: Ok, I just realized, that this is exactly, what the OP did NOT want to do. So this answer is not really useful...

- 9,362
- 9
- 48
- 76
-
Please don't use `System.out.println()` as your exception handling. Not even in example code. *Especially* not in example code. This gets copied into production code so often that it's no longer even funny. – Joachim Sauer Jun 11 '12 at 14:50
-
@JoachimSauer You are completely right. Bad brimborium. Changed it. ;) – brimborium Jun 11 '12 at 14:53