Byte[] to InputStream or OutputStream

http://stackoverflow.com/questions/2091454/byte-to-inputstream-or-outputstream

I'm assuming you mean that 'use' means read, but what i'll explain for the read case can be basically reversed for the write case.

so you end up with a byte[]. this could represent any kind of data which may need special types of conversions (character, encrypted, etc). let's pretend you want to write this data as is to a file.

firstly you could create a ByteArrayInputStream which is basically a mechanism to supply the bytes to something in sequence.

then you could create a FileOutputStream for the file you want to create. there are many types of InputStreams and OutputStreams for different data sources and destinations.

lastly you would write the InputStream to the OutputStream. in this case, the array of bytes would be sent in sequence to the FileOutputStream for writing. For this i recommend using IOUtils

byte[] bytes = ...;//
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
FileOutputStream out = new FileOutputStream(new File(...));
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);

and in reverse

FileInputStream in = new FileInputStream(new File(...));
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
byte[] bytes = out.toByteArray();

if you use the above code snippets you'll need to handle exceptions and i recommend you do the 'closes' in a finally block.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码有什么问题 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;import org.apache.commons.compress.archivers.zip.ZipFile;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;public class UnzipFolder { public static void main(String[] args) { String folderPath = "/path/to/folder"; // 文件夹路径 String password = "password"; // 解压密码 File folder = new File(folderPath); if (folder.exists() && folder.isDirectory()) { File[] files = folder.listFiles(); for (File file : files) { if (file.getName().endsWith(".zip")) { // 如果是zip文件 unzip(file, password); } } } else { System.out.println("Folder does not exist or is not a directory."); } } public static void unzip(File file, String password) { byte[] buffer = new byte[1024]; try { ZipFile zipFile = new ZipFile(file); zipFile.setPassword(password); String destDirPath = file.getParent(); for (ZipArchiveEntry entry : zipFile.getEntries()) { String entryName = entry.getName(); InputStream inputStream = zipFile.getInputStream(entry); File destFile = new File(destDirPath, entryName); if (entry.isDirectory()) { destFile.mkdirs(); } else { FileOutputStream outputStream = new FileOutputStream(destFile); int len; while ((len = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, len); } outputStream.close(); } inputStream.close(); } zipFile.close(); System.out.println("Unzip " + file.getName() + " successfully."); } catch (IOException e) { System.out.println("Unzip " + file.getName() + " failed."); e.printStackTrace(); } }}
最新发布
06-04

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值