InputStream读取文件 InputStreamjavabyte 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ByteArrayOutputStream 有缓冲区,上面那种写法没有 第一种写法对吗? 这种能得到InputStream输入流的所有字节并返回吗? 一般带缓冲区的记得最好调用flush(),要不内容有可能读不出来 你把buffer改大就能看出问题了。 byte[] buffer = new byte[1024 * 1024]; 再用这个buffer来读你输入的6个字符。 一般程序设置的buffer都是固定大小的,不会根据你输入的数据大小再去设定buffer, 那么很容易出现buffer读不满的情况,第二总写法就适用了 第二种是不是要把if换成while,就可以保证数据全部从流中读出 如果要真正的和第二种的写法返回一样的结果,则第一种写法应该类似这样的public class StreamTool { public static byte[] readInputStream(InputStream inputStream) throws Exception { int bigLen = 1024; byte[] maxBuffer = new byte[bigLen]; byte[] buffer = new byte[8]; // ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int length = -1; int totalLen = 0; if ((length = inputStream.read(buffer)) != -1) { if(totalLen + length > bigLen){ bigLen += 512; byte[] temp = new byte[bigLen]; System.arraycopy(maxBuffer, 0, temp, 0, totalLen); maxBuffer = temp; } System.arraycopy(buffer, 0, maxBuffer, totalLen, length); totalLen +=length; } return maxBuffer; }}如果需要读取的字节很多,就要不断的扩充数组,并拷贝数据,性能上会有很多影响 第二种需要把if改成while吧。恩,手误 都要改成while 偶然报名的比赛 发现里面最pl的mm 请问hashCode()具体有什么用? 请高手指点:能用jcreator编写swt程序吗? 请给一个非小应用程序,装载一个图片的完整代码! 请问如何随意的合并JTable的Cell。 如何去掉JInternalFrame的边框 请问怎么在static的方法里调用另一个方法? 紧急求助:没有网卡的pc,如何安装网络协议 设计名为MyPoint的类 java大整数输出一行不够怎么办 adt-elicpse快捷键的使用 Timer和TimerTask问题
public static byte[] readInputStream(InputStream inputStream)
throws Exception {
int bigLen = 1024;
byte[] maxBuffer = new byte[bigLen];
byte[] buffer = new byte[8];
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int length = -1;
int totalLen = 0;
if ((length = inputStream.read(buffer)) != -1) {
if(totalLen + length > bigLen){
bigLen += 512;
byte[] temp = new byte[bigLen];
System.arraycopy(maxBuffer, 0, temp, 0, totalLen);
maxBuffer = temp;
}
System.arraycopy(buffer, 0, maxBuffer, totalLen, length);
totalLen +=length;
}
return maxBuffer;
}
}如果需要读取的字节很多,就要不断的扩充数组,并拷贝数据,性能上会有很多影响
恩,手误 都要改成while