java 内存中创建文件,如何在内存中创建新的java.io.File?

该问题探讨如何在Java中不使用硬盘临时文件,直接从内存中创建JarFile对象。作者面临一个期望File参数的不良API,并希望避免创建临时文件来验证Jar的签名。解决方案建议使用ByteArrayOutputStream在内存中写入,以避免磁盘操作。
摘要由CSDN通过智能技术生成

How can I create new File (from java.io) in memory, not on the hard disk?

I am using the Java language. I don't want to save the file on the hard drive.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值