Java RandomAccessFile在附加的堆栈跟踪中获取资源但从未释放

时间:2016-11-07 03:20:10

标签: java android

好的,这是一个奇怪的。

我有一个Android应用程序,它保存用户在文件中键入的所有内容的缓冲区(buffered.log)。一切都很完美,除了在客户在以前安装过的某些手机上安装应用程序的奇怪情况。

该应用程序然后抛出此错误:

03 20:20:34.678 22741-22749/com.menny.android.anysoftkeyboard E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
                                                                             java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:180)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:127)
at com.radicalninja.logger.FlushableCipherOutputStream.<init>(FlushableCipherOutputStream.java:50)
at com.radicalninja.logger.CipherUtils.flushableEncryptedBufferedWriter(CipherUtils.java:31)
at com.radicalninja.logger.FileWriter.openFileWriter(FileWriter.java:41)
at com.radicalninja.logger.FileWriter.<init>(FileWriter.java:30)
at com.radicalninja.logger.FileWriter.<init>(FileWriter.java:0)
at com.radicalninja.logger.LogManager.openExternalPublicStorage(LogManager.java:161)
at com.radicalninja.logger.LogManager.createFileWriter(LogManager.java:115)
at com.radicalninja.logger.LogFileController.openLogFile(LogFileController.java:18)
at com.radicalninja.logger.LogFileController.<init>(LogFileController.java:12)
at com.radicalninja.logger.Buffer.<init>(Buffer.java:0)
at com.radicalninja.logger.WordBuffer.<init>(WordBuffer.java:0)
at com.anysoftkeyboard.AnySoftKeyboard.onCreate(AnySoftKeyboard.java:328)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3808)
at android.app.ActivityThread.access$2100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1883)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我的问题是:我如何检查随机访问文件是否未正确关闭,并在应用尝试创建之前安装已使用的同一新RandomAccessFile之前将其关闭?

1 个答案:

答案 0 :(得分:1)

好的,这是我解决这个问题的方法:

在应用首次打开时创建具有相同路径的新文件,然后将其删除。

似乎工作,但似乎并不优雅