我不确定我该怎么做。任何帮助都将不胜感激
发布于 2010-08-17 02:17:34
从输入流中读取并写入ByteArrayOutputStream,然后调用其toByteArray()以获取字节数组。
在字节数组周围创建一个ByteArrayInputStream,以便从中读取数据。
下面是一个快速测试:
import java.io.*;
public class Test {
public static void main(String[] arg) throws Throwable {
File f = new File(arg[0]);
InputStream in = new FileInputStream(f);
byte[] buff = new byte[8000];
int bytesRead = 0;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
while((bytesRead = in.read(buff)) != -1) {
bao.write(buff, 0, bytesRead);
}
byte[] data = bao.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(data);
System.out.println(bin.available());
}
}发布于 2019-10-17 18:42:30
您可以使用org.apache.commons.io.IOUtils#toByteArray(java.io.InputStream)
InputStream is = getMyInputStream();
ByteArrayInputStream bais = new ByteArrayInputStream(IOUtils.toByteArray(is));发布于 2012-11-07 20:55:54
或者先将其转换为字节数组,然后转换为bytearrayinputstream。
File f = new File(arg[0]);
InputStream in = new FileInputStream(f);
// convert the inpustream to a byte array
byte[] buf = null;
try {
buf = new byte[in.available()];
while (in.read(buf) != -1) {
}
} catch (Exception e) {
System.out.println("Got exception while is -> bytearr conversion: " + e);
}
// now convert it to a bytearrayinputstream
ByteArrayInputStream bin = new ByteArrayInputStream(buf);https://stackoverflow.com/questions/3495942
复制相似问题