[英]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.