繁体   English   中英

Android FileInputStream连续读取相同数据

[英]Android FileInputStream read same data continuously

我想使蓝牙套接字通信发送文件

我从FileInputStream通过缓冲区读取数据,并将其写入其他输出流。

但是该程序连续读取相同的数据,而不读取下一个内容。

这是我的资料

        MSendArgWrapper wrapper = makeWrapper(sendArg, MSendArgWrapper.MODE_SWITCH_FILE);
        try {
            byte[] bytes = CUtility.serialize(wrapper);
            outStream.write(bytes);
            outStream.flush();
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            byte buf[] = new byte[1024];
            do {
                int numread = fis.read(buf);
                if (numread <= 0)
                    break;
                if(LOG_I_ENABLE)
                    Log.i(TAG, "[CCommunicateThread] Sending File... [" + numread + "] => " + buf.toString());
                outStream.write(buf, 0, numread);
            } while (true);
            outStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
            onDisconnected();
        }

这是日志

08-16 08:07:21.002  20001-21388/com.example.park.psyche I/CBluetoothManager﹕ [CCommunicateThread] Sending File... [1024] => [B@42b0ab48
08-16 08:07:21.002  20001-21388/com.example.park.psyche I/CBluetoothManager﹕ [CCommunicateThread] Sending File... [1024] => [B@42b0ab48
08-16 08:07:21.002  20001-21388/com.example.park.psyche I/CBluetoothManager﹕ [CCommunicateThread] Sending File... [1024] => [B@42b0ab48
08-16 08:07:21.002  20001-21388/com.example.park.psyche I/CBluetoothManager﹕ [CCommunicateThread] Sending File... [1024] => [B@42b0ab48
08-16 08:07:21.002  20001-21388/com.example.park.psyche I/CBluetoothManager﹕ [CCommunicateThread] Sending File... [1024] => [B@42b0ab48
08-16 08:07:21.002  20001-21388/com.example.park.psyche I/CBluetoothManager﹕ [CCommunicateThread] Sending File... [1024] => [B@42b0ab48
08-16 08:07:21.002  20001-21388/com.example.park.psyche I/CBluetoothManager﹕ [CCommunicateThread] Sending File... [1024] => [B@42b0ab48

问题是什么?

您的代码有效,但您打印的内容与您想像的不一样。

字节数组类型byte[]是对象类型。 如果使用方法toString() ,它将不会将字节转换为String。

如果要将byte[]转换为String类型,请使用: new String(my_byte_array)

与:

Log.i(TAG, "[CCommunicateThread] Sending File... [" + numread + "] => " + new String(buf));

您将获得正确的输出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM