如何从输入流中获取字符串以使其成为通过蓝牙传输的文件的名称?
发送文件时,我将文件名添加到outputStream,然后添加" |"作为分隔符,然后我将文件字节数组:
try {
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis, 8 * 1024);
bis.read(mybytearray, 0, mybytearray.length);
outStream.write(fileNameOut.getBytes());
outStream.write("|".getBytes());
outStream.write(mybytearray, 0, mybytearray.length);
outStream.flush();
outStream.close();
} catch (IOException e) {
Log.e("BT Connected", "disconnected", e);
}
收到文件时,我希望得到所有字节,直到" |"字符并将它们保存为字符串,以便成为将写入蓝牙传输字节的新文件的文件名:
try {
try {
int read;
// obtain a filename from inputStream
BufferedInputStream bis = new BufferedInputStream(inStream);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while (result != -1) {
buf.write((byte) result);
result = bis.read();
}
//
general.log("Bluetooth Connected", "file name: " + buf.toString());
while ((read = inStream.read(buffer)) != -1) {
transferStatus = "sending";
bytesRead += read;
general.log("Bluetooth Connected", "odebrano: " + bytesRead + "bytes: ");
fos.write(buffer, 0, read);
transferStatus = "sent";
}
} finally {
outStream.flush();
inStream.close();
mmSocket.close();
}
} catch (Exception e) {
general.log("Bluetooth Connected", "======= Error receiving file: " + e);
}
提交的代码不会查找" |"字符,因为它甚至没有将字节转换为字符串,我不知道为什么。如何将缓冲流分为两部分:获取字符串直到" |"并保存为文件名,得到其余的并写入文件?