简体   繁体   English

Android FileInputStream连续读取相同数据

[英]Android FileInputStream read same data continuously

I want to make bluetooth socket communication sending files 我想使蓝牙套接字通信发送文件

I read data by buffer from FileInputStream, and write it on other output stream. 我从FileInputStream通过缓冲区读取数据,并将其写入其他输出流。

But this program read same data continuously, not read next content. 但是该程序连续读取相同的数据,而不读取下一个内容。

here is my source 这是我的资料

        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();
        }

This is the log 这是日志

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

What is the problem? 问题是什么?

Your code is working but what you print is not what you think it is. 您的代码有效,但您打印的内容与您想像的不一样。

A byte array type byte[] is an Object type. 字节数组类型byte[]是对象类型。 If you use method toString() it will not convert bytes to String. 如果使用方法toString() ,它将不会将字节转换为String。

If you want to convert a byte[] to a String type use : new String(my_byte_array) 如果要将byte[]转换为String类型,请使用: new String(my_byte_array)

with : 与:

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

You'll get the right output 您将获得正确的输出

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

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