Android zipFile java.io.Closeable for information on avoiding resource leaks5
Android zipFile java.io.Closeable for information on avoiding resource leaks5报错下面的错误....
01-05 23:43:46.970: E/StrictMode(28840): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
01-05 23:43:46.970: E/StrictMode(28840): java.lang.Throwable: Explicit termination method 'close' not called
01-05 23:43:46.970: E/StrictMode(28840): at dalvik.system.CloseGuard.open(CloseGuard.java:184)
01-05 23:43:46.970: E/StrictMode(28840): at java.io.RandomAccessFile.<init>(RandomAccessFile.java:128)
01-05 23:43:46.970: E/StrictMode(28840): at java.io.RandomAccessFile.<init>(RandomAccessFile.java:150)
01-05 23:43:46.970: E/StrictMode(28840): at java.util.zip.ZipFile.<init>(ZipFile.java:130)
01-05 23:43:46.970: E/StrictMode(28840): at java.util.zip.ZipFile.<init>(ZipFile.java:104)
01-05 23:43:46.970: E/StrictMode(28840): at org.jeffliu.test.ExtractXml.getThemeDrawableStream(ExtractXml.java:75)
01-05 23:43:46.970: E/StrictMode(28840): at org.jeffliu.test.ExtractXml.listFile(ExtractXml.java:56)
01-05 23:43:46.970: E/StrictMode(28840): at org.jeffliu.test.ExtractXml.onCreate(ExtractXml.java:42)
01-05 23:43:46.970: E/StrictMode(28840): at android.app.Activity.performCreate(Activity.java:5122)
01-05 23:43:46.970: E/StrictMode(28840): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
01-05 23:43:46.970: E/StrictMode(28840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
01-05 23:43:46.970: E/StrictMode(28840): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
01-05 23:43:46.970: E/StrictMode(28840): at android.app.ActivityThread.access$600(ActivityThread.java:156)
01-05 23:43:46.970: E/StrictMode(28840): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
01-05 23:43:46.970: E/StrictMode(28840): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 23:43:46.970: E/StrictMode(28840): at android.os.Looper.loop(Looper.java:153)
01-05 23:43:46.970: E/StrictMode(28840): at android.app.ActivityThread.main(ActivityThread.java:5297)
01-05 23:43:46.970: E/StrictMode(28840): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 23:43:46.970: E/StrictMode(28840): at java.lang.reflect.Method.invoke(Method.java:511)
01-05 23:43:46.970: E/StrictMode(28840): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-05 23:43:46.970: E/StrictMode(28840): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-05 23:43:46.970: E/StrictMode(28840): at dalvik.system.NativeStart.main(Native Method)
01-05 23:43:46.989: E/ANRAppManager(28840): RecordRateNotMatch: Count:MsgRecordSize:upTimeSize:elapsedTimeSize = 188:6:12:12
代码片段:
ZipFile zipFile=null;
try {
zipFile = new ZipFile("test.xml");//传入的不是个压缩文件
} catch (IOException e) {
System.out.println("[ThemeResource][InputStream]error =>" + e.getMessage());
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(null!=zipFile){
zipFile.close();//传入的不是压缩文件时,ZipFile的构造方法没有执行完zipFile=null,无法执行到这里
zipFile=null;
}
} catch (IOException e2) {
}
}
ZipFile构造方法
Zipfile构造方法代码 收藏代码
public ZipFile(File file, int mode) throws IOException {
filename = file.getPath();
if (mode != OPEN_READ && mode != (OPEN_READ | OPEN_DELETE)) {
throw new IllegalArgumentException("Bad mode: " + mode);
}
if ((mode & OPEN_DELETE) != 0) {
fileToDeleteOnClose = file;
fileToDeleteOnClose.deleteOnExit();
} else {
fileToDeleteOnClose = null;
}
raf = new RandomAccessFile(filename, "r");
<span style="color: #ff6600;">readCentralDir();//异常在这里抛出,源码上没有地方看到调用raf的close方法。</span>
guard.open("close");
}
RandomAccessFile构造方法:
public RandomAccessFile(File file, String mode) throws FileNotFoundException {
int flags;
if (mode.equals("r")) {
flags = O_RDONLY;
} else if (mode.equals("rw") || mode.equals("rws") || mode.equals("rwd")) {
flags = O_RDWR | O_CREAT;
if (mode.equals("rws")) {
// Sync file and metadata with every write
syncMetadata = true;
} else if (mode.equals("rwd")) {
// Sync file, but not necessarily metadata
flags |= O_SYNC;
}
} else {
throw new IllegalArgumentException("Invalid mode: " + mode);
}
this.mode = flags;
this.fd = IoBridge.open(file.getAbsolutePath(), flags);
// if we are in "rws" mode, attempt to sync file+metadata
if (syncMetadata) {
try {
fd.sync();
} catch (IOException e) {
// Ignored
}
}
guard.open("close");//............
}
这种问题怎么解?? Android 压缩 zipFile
补充:移动开发 , Android