如何在内存中创建新的java.io.File? [英] how to create new java.io.File in memory?
问题描述
如何在内存中创建新的文件
(来自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文件名的File文件。我没有文件(只有byte []内容)并且可以创建临时文件,但它不是很漂亮的解决方案。我需要验证签名jar的摘要。
I'm faced with 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 digest of 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 temporary file would be appreciated.
我认为现在这个问题不是offtopic
I think now this question is not offtopic
推荐答案
要写入流,在内存中,使用:
To write to a stream, in memory, use:
new ByteArrayOutputStream();
这篇关于如何在内存中创建新的java.io.File?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!