如果我有一个包含我想要的确切byte[]
数据文件:读取字节值在文件转换成字节数组
[-119, 45, 67, ...]
我怎么能读取这些值转换为byte
阵列?是否必须将它们作为String
读取,然后将这些String
值转换为byte
值?
这是目前我在做什么,和它的作品,但慢慢地:
BufferedReader reader = null;
String mLine = null;
AssetManager assetManager = CustomApplication.getCustomAppContext().getAssets();
try {
reader = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
mLine = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String[] byteValues = mLine.substring(1, mLine.length() - 1).split(",");
byte[] imageBytes = new byte[byteValues.length];
for (int i = 0; i < imageBytes.length; i++) {
imageBytes[i] = Byte.valueOf(byteValues[i].trim());
}
return imageBytes;
UPDATE:似乎有是什么,我想完成一个误区。我的文件包含我想要的最终字节数组中的EXACT字节数据。例如,我想将文件转换与准确
[-119, 45, 67, ...]
成byte[]
称为结果与
result[0] = -119
result[1] = 45
result[2] = 67
读一个字节整个文件陆续真的不好,很慢的练习。 –
这不起作用。当以字符串形式显示时,我的缓冲区将包含正确的值[[-119,45,67,...]',但在'toByteArray()'后面,结果包含'[91,45,49,...] 。在调试中,缓冲区的字符串表示是正确的,但实际值是错误的。 –