1、每次少读一点放到StringBuffer中,这样就可以解决一次性定义很大内存的问题。
2、最后得到一个StringBuffer,然后转换成String,在转换成byte[]
用途:xml文件传输。
代替方案:文件传输的时候,把文件分割传输
断点续传
使用ByteArrayOutputStream
public class UseByteArrayOutputStream(){
public static void main(String[] args) throws IOException{
Url url = UseByteArrayOutputStream.class.getResource("a.xml");
File file = new File(url.getFile());
InputStream inPutStream = new FileInputStream(file);
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int len;
byte[] b = new byte[1024];
while(len = inputStream.read(b) != -1){
bytestream.write(b,o,len);
}
byte[] bytes = bytestream.toByteArray();
bytestream.close();
}
}