FileInputstream android 垃圾

发布于 2024-12-14 21:22:39 字数 416 浏览 2 评论 0原文

我正在使用此代码来读取输入流,但它不起作用它在缓冲区中给出了垃圾值。下面的代码有什么问题: //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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身狗的梦 2024-12-21 21:22:39

可以尝试这段代码:

BufferedReader r = new BufferedReader(new InputStreamReader(mInputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
    total.append(line);
}
l("String: " + total);

并发布输出吗?

Could try this code:

BufferedReader r = new BufferedReader(new InputStreamReader(mInputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
    total.append(line);
}
l("String: " + total);

and post the output?

雨夜星沙 2024-12-21 21:22:39

如您所知,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文