我一直得到java.io.IOException:Stream Closed,我似乎无法理解为什么。我在按钮推送(下面显示的对象)上创建了两个SensorReadings对象(每个对象被发送一个用于文件名的唯一字符串),它打开两个ObjectoutputStream并写入两个单独的文件。这是我第一次使用它时完美无缺,没有任何记忆。当我写完文件时,我按下另一个关闭Objectoutputstreams的按钮并删除我写的文件。
当我再次尝试使用新的Objectoutputstreams和具有不同名称的新文件来按下创建新SensorReadings对象的按钮时,我会遇到java.io.IOException:每次尝试写入文件时都会关闭流。
关闭应用程序并重新启动它将导致第一次完美地工作,但是周围的时间我得到同样的错误。
代码:
public class SensorReadings {
private String macAddress;
private String cacheFileAccelerationName;
private String cacheFileEulerAnglesName;
private File cacheFileAcceleration;
private File cacheFileEulerAngles;
private ObjectOutputStream accelerationStreamWriter;
private ObjectOutputStream euleranglesStreamWriter;
public SensorReadings(String macAddress, Context context) {
SimpleDateFormat dateFormata = new SimpleDateFormat("yyyy.MM.dd'-'HH:mm:ss");
this.macAddress = macAddress;
cacheFileAccelerationName = dateFormata.format(Calendar.getInstance().getTime())+macAddress+"-acceleration-cache";
cacheFileEulerAnglesName = dateFormata.format(Calendar.getInstance().getTime())+macAddress+"-eulerangles-cache";
cacheFileAcceleration = new File(context.getCacheDir(), cacheFileAccelerationName);
cacheFileEulerAngles = new File(context.getCacheDir(), cacheFileEulerAnglesName);
try {
accelerationStreamWriter = new ObjectOutputStream(new FileOutputStream(cacheFileAcceleration));
euleranglesStreamWriter = new ObjectOutputStream(new FileOutputStream(cacheFileEulerAngles));
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
public void addAccelerationData(AccelerationReading reading){
try {
accelerationStreamWriter.writeUnshared(reading);
accelerationStreamWriter.reset();
} catch (IOException e) {
e.printStackTrace();
}
}
public void addEulerAngleData(EulerAnglesReading reading){
try {
euleranglesStreamWriter.writeUnshared(reading);
euleranglesStreamWriter.reset();
} catch (IOException e) {
e.printStackTrace();
}
public void closeFiles(){
try {
euleranglesStreamWriter.flush();
accelerationStreamWriter.flush();
euleranglesStreamWriter.close();
accelerationStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
cacheFileEulerAngles.delete();
cacheFileAcceleration.delete();
}
}
每次我在创建新对象后尝试编写时都会出错:
03-27 21:00:38.526 9882-9894/com.appitude.appitude W/System.err: java.io.IOException: Stream Closed
03-27 21:00:38.526 9882-9894/com.appitude.appitude W/System.err: at java.io.FileOutputStream.write(FileOutputStream.java:310)
03-27 21:00:38.526 9882-9894/com.appitude.appitude W/System.err: at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1946)
03-27 21:00:38.526 9882-9894/com.appitude.appitude W/System.err: at java.io.ObjectOutputStream$BlockDataOutputStream.writeByte(ObjectOutputStream.java:1986)
03-27 21:00:38.526 9882-9894/com.appitude.appitude W/System.err: at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1612)
03-27 21:00:38.526 9882-9894/com.appitude.appitude W/System.err: at java.io.ObjectOutputStream.writeUnshared(ObjectOutputStream.java:432)
03-27 21:00:38.526 9882-9894/com.appitude.appitude W/System.err: at com.appitude.appitude.SensorReadings.addAccelerationData(SensorReadings.java:103)
03-27 21:00:38.526 9882-9894/com.appitude.appitude W/System.err: at com.appitude.appitude.BLE_Connections.lambda$null$31d3e978$1$BLE_Connections(BLE_Connections.java:265)
03-27 21:00:38.526 9882-9894/com.appitude.appitude W/System.err: at com.appitude.appitude.BLE_Connections$$Lambda$24.apply(Unknown Source)
03-27 21:00:38.526 9882-9894/com.appitude.appitude W/System.err: at com.mbientlab.metawear.impl.DeviceDataConsumer.call(DeviceDataConsumer.java:51)
03-27 21:00:38.527 9882-9894/com.appitude.appitude W/System.err: at com.mbientlab.metawear.impl.StreamedDataConsumer.lambda$addDataHandler$1$StreamedDataConsumer(StreamedDataConsumer.java:138)
03-27 21:00:38.527 9882-9894/com.appitude.appitude W/System.err: at com.mbientlab.metawear.impl.StreamedDataConsumer$$Lambda$1.onResponseReceived(Unknown Source)
03-27 21:00:38.527 9882-9894/com.appitude.appitude W/System.err: at com.mbientlab.metawear.impl.JseMetaWearBoard.lambda$null$13$JseMetaWearBoard(JseMetaWearBoard.java:699)
03-27 21:00:38.527 9882-9894/com.appitude.appitude W/System.err: at com.mbientlab.metawear.impl.JseMetaWearBoard$$Lambda$30.onChange(Unknown Source)
03-27 21:00:38.527 9882-9894/com.appitude.appitude W/System.err: at com.mbientlab.metawear.android.BtleService$1.onCharacteristicChanged(BtleService.java:188)
03-27 21:00:38.527 9882-9894/com.appitude.appitude W/System.err: at android.bluetooth.BluetoothGatt$1.onNotify(BluetoothGatt.java:402)
03-27 21:00:38.527 9882-9894/com.appitude.appitude W/System.err: at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:177)
03-27 21:00:38.527 9882-9894/com.appitude.appitude W/System.err: at android.os.Binder.execTransact(Binder.java:573)
答案 0 :(得分:0)
问题解决了。正在传输数据然后写入文件的连接蓝牙设备在流关闭时没有停止,并且在激活新会话时继续尝试使用相同的流。