如何在内存中创建一个新的java.io.File? [英] How to create a new java.io.File in memory?
问题描述
如何在内存中而不是在硬盘上创建新的File
(通过java.io
)?
How can I create new File
(from java.io
) in memory, not on the hard disk?
我正在使用Java语言.我不想将文件保存在硬盘上.
I am using the Java language. I don't want to save the file on the hard drive.
我遇到了一个错误的API(java.util.jar.JarFile
).期望String filename
的File file
.我没有文件(只有byte[]
内容),可以创建临时文件,但这不是一个漂亮的解决方案.我需要验证签名罐的摘要.
I'm faced with a bad API (java.util.jar.JarFile
). It's expecting File file
of String filename
. I have no file (only byte[]
content) and can create temporary file, but it's not beautiful solution. I need to validate the digest of a signed jar.
byte[] content = getContent();
File tempFile = File.createTempFile("tmp", ".tmp");
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(archiveContent);
JarFile jarFile = new JarFile(tempFile);
Manifest manifest = jarFile.getManifest();
在不创建临时文件的情况下获得清单的任何示例都将受到赞赏.
Any examples of how to achieve getting manifest without creating a temporary file would be appreciated.
推荐答案
要在内存中写入流,请使用:
To write to a stream, in memory, use:
new ByteArrayOutputStream();
这篇关于如何在内存中创建一个新的java.io.File?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!