gpt4 book ai didi

java.io.IOException : Stream Closed ObjectOutputStream

转载 作者:太空宇宙 更新时间:2023-11-04 10:35:37 26 4
gpt4 key购买 nike

我不断收到 java.io.IOException: Stream Closed,我似乎无法理解为什么。我在按钮按下(如下所示的对象)上创建两个 SensorReadings 对象(每个对象发送一个用于文件名的唯一字符串),该按钮打开两个 ObjectoutputStream 并写入两个单独的文件。我第一次使用它时,它工作得完美无缺,没有任何异常(exception)。当我完成对文件的写入后,我按下另一个按钮来关闭 Objectoutputstreams 并删除我写入的文件。

当我再次尝试按下按钮,使用新的 Objectoutputstreams 和具有不同名称的新文件创建新的 SensorReadings 对象时,每次尝试写入文件时,我都会遇到 java.io.IOException: Stream Closed。

关闭应用程序并重新启动将导致第一次再次完美工作,但第二次++时我遇到了同样的错误。

代码:

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)

最佳答案

问题已解决。正在传输数据然后写入文件的连接的蓝牙设备在流关闭时不会停止,并在激活新 session 时继续尝试使用相同的流。

关于java.io.IOException : Stream Closed ObjectOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49520682/

26 4 0