简体   繁体   English

在java中将二进制文件转换为字节数组?

[英]Convert a binary file into a byte array in java?

I've previously converted a byte array into a file conataining the binary equivalents of all the values in that byte array.我之前已经将一个字节数组转换为一个文件,该文件包含该字节数组中所有值的二进制等效项。 Now how do do I convert those binary values back into a byte array??现在如何将这些二进制值转换回字节数组?

Like for example...my byte array starts with values 7, 17, 118, 7.... And my text file conatining the binary values shows 00000111000100010111011000000111....例如......我的字节数组以值 7、17、118、7....

Read the file 8 characters at a time and use Integer.parseInt(chars, 2) where chars is the 8 characters you read in as a String.一次读取文件 8 个字符并使用Integer.parseInt(chars, 2)其中chars是您作为字符串读取的 8 个字符。 Repeat until the file is completely read.重复直到文件被完全读取。

use the input stream使用输入 stream

InputStream is = new FileInputStream(new File("filen.ame"));
is.read(byte[] b, 0, len(file))

Input Stream Documentation输入 Stream 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM