0

我的线程将从 jar 文件中读取类数据,另一个线程将修改或删除 jar 文件。订单是先读再删除,但是没用,读完好像不能释放资源,怎么联系?

InputStream is = null;
BufferedInputStream bis = null;
ByteArrayOutputStream baos = null;
try {
URL res = new URL(**file**);
is = res.openStream();
bis = new BufferedInputStream(is);
baos = new ByteArrayOutputStream();

byte[] bytes = new byte[1024 * 10];
int readBytes;

while ((readBytes = bis.read(bytes)) != -1) {
    baos.write(bytes, 0, readBytes);
}
byte[] b = baos.toByteArray();
baos.close();
     bis.close();
     is.close();

return b;
} catch (Exception ex) {
throw ex;
}

参数“file”是这样的字符串“jar:file:///C:/Users/HJ16748/Desktop/test.jar!/com/services/plugin/test/Test.class”

4

2 回答 2

0

将 finally 添加到您的 try catch 块并关闭那里的资源。让两个线程名称为 Read 和 Modify。

在删除或修改 jar 之前在修改线程中添加这样的行。

try{
System.out.println("Waiting for read to finish");
read.thread.join();
}catch(InterruptedException e){
System.out.println("not able to join");//oops catch
}
//codes to delete or modify jar

用 read 调用的线程是 Thread 对象

于 2013-11-06T10:19:34.247 回答
0
ZipFile zf = new ZipFile(entry);
file = file.replaceAll("\\\\", "/");
ZipEntry zipEntry = zf.getEntry(file);

BufferedInputStream bis = new BufferedInputStream(zf.getInputStream(zipEntry));
ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] bytes = new byte[1024 * 10];
int readBytes;

while ((readBytes = bis.read(bytes)) != -1) {
    baos.write(bytes, 0, readBytes);
}
b = baos.toByteArray();

baos.close();
bis.close();
zf.close();
于 2013-11-08T02:31:50.950 回答