Java 输入流到字符串
如何从输入流中获取字符串以使其成为通过蓝牙传输的文件名 发送文件时,我将文件名添加到outputStream,然后添加“|”作为分隔符,然后添加文件字节数组:Java 输入流到字符串,java,android,bluetooth,Java,Android,Bluetooth,如何从输入流中获取字符串以使其成为通过蓝牙传输的文件名 发送文件时,我将文件名添加到outputStream,然后添加“|”作为分隔符,然后添加文件字节数组: try { FileInputStream fis = new FileInputStream(myFile); BufferedInputStream bis = new BufferedInputStream(fis, 8 * 1024); bis.read(m
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);
}
所展示的代码还没有查找“|”字符,因为它甚至没有将字节转换为字符串,我也不知道为什么。如何将缓冲流分为两部分:获取字符串直到“|”并另存为文件名,获取其余部分并写入文件?Hi@jacek_podwysocki,您是否能够通过蓝牙套接字传输文件?请看这个或者分享你的代码好吗?如果你已经这样做了,请,我被卡住了:(Hi@jacek_podwysocki,你能通过蓝牙插座传输文件吗?你能看这个或者分享你的代码吗?如果你已经这样做了,请,我被卡住了:(