FileInputstream android 垃圾
我正在使用此代码来读取输入流,但它不起作用它在缓冲区中给出了垃圾值。下面的代码有什么问题: //mInputStream是FileInputStream的对象
buf = new byte[4];
int ret = 0;
l("run");
try {
while ( (ret = mInputStream.read(buf) )!= -1) {
l("No.of bytes received:"+ret);
l("Data received length :"+buf.length);
l("Data received: "+buf.toString());
}
} catch (IOException e) {
l("IO exception in receiving");
}
I am using this code to read input stream but its not working It s giving garbage value in buffer. what's wrong with the code below:
//mInputStream is an object of FileInputStream
buf = new byte[4];
int ret = 0;
l("run");
try {
while ( (ret = mInputStream.read(buf) )!= -1) {
l("No.of bytes received:"+ret);
l("Data received length :"+buf.length);
l("Data received: "+buf.toString());
}
} catch (IOException e) {
l("IO exception in receiving");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以尝试这段代码:
并发布输出吗?
Could try this code:
and post the output?
如您所知,java 中的每个类都扩展了 Object 类。任何类的每个实例都有一个继承自Object类的toString方法,该方法打印出[类名]@[实例id]。看起来 buf.toString() 返回一个类名 (B) 及其 ID (40533870)。尝试一次检查 buf 的一个索引的内容。
As you know every class in java extends Object class. Every instance of any class has a toString method inherited from the Object class, which prints out [name of class]@[instance id]. It seems like buf.toString() returns a class name (B) and its ID (40533870). Try checking the contents of buf one index at a time.