从字节数组创建文件

时间:2022-09-17 13:32:32

So - I've got a third party library that needs a File as input. I've got a byte array.

所以 - 我有一个需要File作为输入的第三方库。我有一个字节数组。

I don't want to write the bytes to disk .. I'd like to keep this in memory. Any idea on how I can create a File from the provided byte array (without writing to disk)?

我不想把字节写入磁盘..我想把它保存在内存中。关于如何从提供的字节数组创建文件(无需写入磁盘)的任何想法?

3 个解决方案

#1


10  

Sorry, not possible. A File is inherently an on-disk entity, unless you have a RAM disk - but that's not something you can create in Java.

对不起,不可能。文件本质上是一个磁盘上的实体,除非你有一个RAM磁盘 - 但这不是你可以用Java创建的东西。

That's exactly the reason why APIs should not be based on File objects (or be overloaded to accept an InputStream).

这就是为什么API不应该基于File对象(或者重载以接受InputStream)的原因。

#2


3  

There's one possibility, but it's a real long-shot.

有一种可能性,但这是一个真正的长镜头。

If the API uses new FileReader(file) or new FileInputStream(file) then you're hosed, but...

如果API使用新的FileReader(文件)或新的FileInputStream(文件),那么你就被软管了,但......

If it converts the file to a URL or URI (using toURL() or toURI()) then, since File is not final, you can pass in a subclass of File in which you control the construction of the URL/URI and, more importantly, the handler.

如果它将文件转换为URL或URI(使用toURL()或toURI()),那么,由于File不是final,您可以传入File的子类,您可以在其中控制URL / URI的构造,以及更多重要的是,处理程序。

But the chances are VERY slim!

但机会非常渺茫!

#3


0  

So I see there is an accepted answer (and this is old), but I found a way to do this. I was using the IDOL On Demand API and needed to convert a byte array to a File.

所以我看到有一个已接受的答案(这是旧的),但我找到了一种方法来做到这一点。我使用的是IDOL On Demand API,需要将字节数组转换为File。

Here is an example of taking a byte array of an image and turning into a File:

以下是获取图像的字节数组并转换为文件的示例:

//imageByte is the byte array that is already defined
BufferedImage image = null;
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();

// write the image to a file
File outputfile = new File("image.png");
ImageIO.write(image, "png", outputfile);

And so outputfile is a File that can be used later in your program.

因此outputfile是一个可以在程序中稍后使用的文件。

#1


10  

Sorry, not possible. A File is inherently an on-disk entity, unless you have a RAM disk - but that's not something you can create in Java.

对不起,不可能。文件本质上是一个磁盘上的实体,除非你有一个RAM磁盘 - 但这不是你可以用Java创建的东西。

That's exactly the reason why APIs should not be based on File objects (or be overloaded to accept an InputStream).

这就是为什么API不应该基于File对象(或者重载以接受InputStream)的原因。

#2


3  

There's one possibility, but it's a real long-shot.

有一种可能性,但这是一个真正的长镜头。

If the API uses new FileReader(file) or new FileInputStream(file) then you're hosed, but...

如果API使用新的FileReader(文件)或新的FileInputStream(文件),那么你就被软管了,但......

If it converts the file to a URL or URI (using toURL() or toURI()) then, since File is not final, you can pass in a subclass of File in which you control the construction of the URL/URI and, more importantly, the handler.

如果它将文件转换为URL或URI(使用toURL()或toURI()),那么,由于File不是final,您可以传入File的子类,您可以在其中控制URL / URI的构造,以及更多重要的是,处理程序。

But the chances are VERY slim!

但机会非常渺茫!

#3


0  

So I see there is an accepted answer (and this is old), but I found a way to do this. I was using the IDOL On Demand API and needed to convert a byte array to a File.

所以我看到有一个已接受的答案(这是旧的),但我找到了一种方法来做到这一点。我使用的是IDOL On Demand API,需要将字节数组转换为File。

Here is an example of taking a byte array of an image and turning into a File:

以下是获取图像的字节数组并转换为文件的示例:

//imageByte is the byte array that is already defined
BufferedImage image = null;
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();

// write the image to a file
File outputfile = new File("image.png");
ImageIO.write(image, "png", outputfile);

And so outputfile is a File that can be used later in your program.

因此outputfile是一个可以在程序中稍后使用的文件。