有关java的问题
importjava.io.*;classdata流{publicstaticvoidmain(String[]args)throwsException{ByteArra...
import java.io.*;
class data流
{
public static void main (String[] args) throws Exception
{
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dot = new DataOutputStream(bao);
long ln = 123456L;
dot.writeLong(ln);
dot.flush();
byte[] by = bao.toByteArray();
ByteArrayInputStream baos = new ByteArrayInputStream(by);
DataInputStream dots = new DataInputStream(baos);
System.out.println("ln = " + dots.readLong());
ByteArrayInputStream bas = new ByteArrayInputStream(by);
BufferedInputStream bp = new BufferedInputStream(bas);
int len;
while(-1 != bp.read())
{
System.out.println("ln = " +bp.read());
}
}
}
输出结果是:
ln = 123456
ln = 0
ln = 0
ln = 1
ln = 64
求教大神为什么bufferinputstream和dateinputstream输入结果不一样? 谢谢! 展开
class data流
{
public static void main (String[] args) throws Exception
{
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dot = new DataOutputStream(bao);
long ln = 123456L;
dot.writeLong(ln);
dot.flush();
byte[] by = bao.toByteArray();
ByteArrayInputStream baos = new ByteArrayInputStream(by);
DataInputStream dots = new DataInputStream(baos);
System.out.println("ln = " + dots.readLong());
ByteArrayInputStream bas = new ByteArrayInputStream(by);
BufferedInputStream bp = new BufferedInputStream(bas);
int len;
while(-1 != bp.read())
{
System.out.println("ln = " +bp.read());
}
}
}
输出结果是:
ln = 123456
ln = 0
ln = 0
ln = 1
ln = 64
求教大神为什么bufferinputstream和dateinputstream输入结果不一样? 谢谢! 展开
1个回答
展开全部
DataInputStream的readLong方法返回的是:the next eight bytes of this input stream, interpreted as a long (将下8bytes的数据流转换为long型返回),但是BufferedInputStream返回的是the next byte of data, or -1 if the end of the stream is reached(下一byte的数据,如果是-1则到达数据结尾了)。所以返回的肯定不一样了。
追问
没看懂有什么不一样。。。。还能更清楚一点吗
追答
我改了下你的代码,我觉得运行完了以后你会明白的。
import java.io.*;
public class DataInputStreamTest {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dot = new DataOutputStream(bao);
long ln = 1234567L;
dot.writeLong(ln);
dot.flush();
byte[] by = bao.toByteArray();
// Data Start
ByteArrayInputStream baos = new ByteArrayInputStream(by);
DataInputStream dots = new DataInputStream(baos);
System.out.println("data: ln = " + dots.readLong());
// Data End
// Buffer Start
ByteArrayInputStream bas = new ByteArrayInputStream(by);
BufferedInputStream bp = new BufferedInputStream(bas);
StringBuffer bufferStringbuffer = new StringBuffer("");
int len =0;
while(true){
len ++;
bufferStringbuffer.append("+"+bp.read()+"*256^("+(8-len)+")");
if(len==8){break;}
}
System.out.println("sum="+bufferStringbuffer.substring(1).toString());
// Buffer End
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |