写入失败:EBADF(错误文件描述符)-在文件上写入错误

时间:2019-06-18 13:37:08

标签: android android-file

我正在开发一个应用程序,该应用程序每次从传感器接收到新数据时就写入文件(我每秒接收到266ish浮点数),并且我必须存储每一分钟的数据,因此每一分钟m创建一个全新的文件并在上面写,有时,几个小时后,我得到了错误的文件描述符,我不知道到底发生了什么。通常会在至少2:30h之后发生,但可能会持续更长的时间

    fun startMonitoring() {
        createNewFhirPackageForCurrentMonitoring {
            currentFhirPackage = it
            createNewFileManagers(it)
            shouldWriteLogFile = true
            startFhirPackageChangerRoutine()
        }
    }


  @Synchronized
    private fun createNewFileManagers(fhirPackage: FhirPackage) {
        val currentMonitoring = currentMonitoring ?: return
        val folder = filesDir
                .resolve(currentMonitoring.monitoringName)
                .resolve(fhirPackage.packageIdentifier.toString())
        dataFilesManager = BraincareDataLogFilesManager(folder)
    }

///这是我每分钟更改文件的地方

private fun startFhirPackageChangerRoutine() {
        fhirPackageChangerSubscription = Observable.interval(FHIR_DURATION, FHIR_DURATION, TimeUnit.SECONDS).flatMap {
            Observable.fromCallable {
                changeFhirPackage()
            }
        }.subscribe({
        }, {
            BleLogHelper.writeError("Error on Monitoring Package Change", it)
        })
    }

 private fun changeFhirPackage() {
        val closingFhirPackage = currentFhirPackage ?: return
        val closingDataFilesManager = dataFilesManager

        createNewFhirPackageForCurrentMonitoring {
            currentFhirPackage = it
            createNewFileManagers(it)
            closingDataFilesManager?.closeOutputStreams()
        }
    }

//我将继续使用此功能写文件

private var differentialBridgeSignalOutputStream: ObjectOutputStream? = null
    fun logDifferentialBridgeSignal(signal: BraincareBluetoothDataFrame.DifferentialBridgeSignal) {
        try {
            if (differentialBridgeSignalOutputStream == null) {
                val file = File(folder, differentialBridgeSignalFileName)
                file.parentFile.mkdirs()
                file.createNewFile()

                differentialBridgeSignalOutputStream = ObjectOutputStream(FileOutputStream(file))
            }
            differentialBridgeSignalOutputStream?.writeObject(signal)
        } catch (e: IOException) {
            BleLogHelper.writeError("Error writing DifferentialBridgeSignal data $signal to $differentialBridgeSignalOutputStream $e", e)
        }
    }

错误日志

java.io.IOException: write failed: EBADF (Bad file descriptor)
            at libcore.io.IoBridge.write(IoBridge.java:558)
            at java.io.FileOutputStream.write(FileOutputStream.java:326)
            at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1959)
            at java.io.ObjectOutputStream$BlockDataOutputStream.writeByte(ObjectOutputStream.java:1999)
            at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1475)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1227)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
            at io.b4c.brain4care.modules.monitoringservice.filesmanagers.BraincareStatusLogFilesManager.logSystemInformation(BraincareStatusLogFilesManager.kt:32)
            at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService.handleStatusFrame(BraincareMonitoringService.kt:257)
            at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService.access$handleStatusFrame(BraincareMonitoringService.kt:45)
            at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService$LocalBinder$clearAndPrepareMonitoringService$2.invoke(BraincareMonitoringService.kt:158)
            at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService$LocalBinder$clearAndPrepareMonitoringService$2.invoke(BraincareMonitoringService.kt:102)
            at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.processFrame(BraincareBluetoothManager.kt:797)
            at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.handleBluetoothNotification(BraincareBluetoothManager.kt:724)
            at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.access$handleBluetoothNotification(BraincareBluetoothManager.kt:40)
            at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager$startReadingSensorNotification$3.accept(BraincareBluetoothManager.kt:632)
            at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager$startReadingSensorNotification$3.accept(BraincareBluetoothManager.kt:40)
            at io.reactivex.internal.observers.LambdaObserver.onNext(LambdaObserver.java:63)
            at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:201)
            at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:255)
            at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
            at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
            at java.lang.Thread.run(Thread.java:764)
         Caused by: android.system.ErrnoException: write failed: EBADF (Bad file descriptor)
            at libcore.io.Linux.writeBytes(Native Method)
            at libcore.io.Linux.write(Linux.java:286)
            at libcore.io.BlockGuardOs.write(BlockGuardOs.java:345)
            at libcore.io.IoBridge.write(IoBridge.java:553)
            at java.io.FileOutputStream.write(FileOutputStream.java:326) 
            at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1959) 
            at java.io.ObjectOutputStream$BlockDataOutputStream.writeByte(ObjectOutputStream.java:1999) 
            at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1475) 
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1227) 
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347) 
            at io.b4c.brain4care.modules.monitoringservice.filesmanagers.BraincareStatusLogFilesManager.logSystemInformation(BraincareStatusLogFilesManager.kt:32) 
            at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService.handleStatusFrame(BraincareMonitoringService.kt:257) 
            at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService.access$handleStatusFrame(BraincareMonitoringService.kt:45) 
            at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService$LocalBinder$clearAndPrepareMonitoringService$2.invoke(BraincareMonitoringService.kt:158) 
            at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService$LocalBinder$clearAndPrepareMonitoringService$2.invoke(BraincareMonitoringService.kt:102) 
            at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.processFrame(BraincareBluetoothManager.kt:797) 
            at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.handleBluetoothNotification(BraincareBluetoothManager.kt:724) 
            at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.access$handleBluetoothNotification(BraincareBluetoothManager.kt:40) 
            at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager$startReadingSensorNotification$3.accept(BraincareBluetoothManager.kt:632) 
            at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager$startReadingSensorNotification$3.accept(BraincareBluetoothManager.kt:40) 
            at io.reactivex.internal.observers.LambdaObserver.onNext(LambdaObserver.java:63) 
            at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:201) 
            at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:255) 
            at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66) 
            at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57) 
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301) 
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
            at java.lang.Thread.run(Thread.java:764) 

什么可能导致此错误,竞赛条件?它之所以难以置信,是因为它在何时会发生上非常随机

0 个答案:

没有答案