当前位置:网站首页>InputStream流已经关闭了,但是依旧无法delete文件或者文件夹,提示被JVM占用等
InputStream流已经关闭了,但是依旧无法delete文件或者文件夹,提示被JVM占用等
2022-06-25 22:06:00 【langmeng110】
有时候流明明已经关闭了,但是还是无法删除文件或者文件夹,提示被JVM占用等
public void download(String sourceUrl, String targetPathFile) {
URL url = null;
//从网络上下载一张图片
InputStream inputStream = null;
OutputStream outputStream = null;
//建立一个网络链接
HttpURLConnection con = null;
try {
url = new URL(sourceUrl);
con = (HttpURLConnection) url.openConnection();
inputStream = con.getInputStream();
outputStream = new FileOutputStream(new File(targetPathFile));
int n = -1;
byte b[] = new byte[1024];
while ((n = inputStream.read(b)) != -1) {
outputStream.write(b, 0, n);
}
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(inputStream!=null)
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
if(outputStream!=null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}这样的时候,通常可以这样解决:
System.gc();//在删除之前调用垃圾回收,这样jvm就不会占用文件了
try {
Thread.sleep(100);//我这边是这样,如果不休眠一会,依旧无法删除
} catch (InterruptedException e) {
e.printStackTrace();
}
boolean dirFile = FileUtils.deleteQuietly(new File(targetDirPath));
boolean zipFile = FileUtils.deleteQuietly(new File(targetZipPathFile));
边栏推荐
猜你喜欢

STL教程5-STL基本概念及String和vector使用

如何设计产品的roadmap?

CXF

YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21

达梦数据库修改字段信息采坑记

hiberate架构介绍及环境搭建(非常详细)

Once beego failed to find bee after passing the go get command Exe's pit

When are the three tools used for interface testing?

为什么Integer的比较最好使用equals

经典图像分割网络:Unet 支持libtorch部署推理【附代码】
随机推荐
对卡巴斯基发现的一个将shellcode写入evenlog的植入物的复现
mysql
CSDN add on page Jump and off page specified paragraph jump
A. Balance the Bits--Codeforces Round #712 (Div. 1)
hiberate实体类CURD、事务操作汇总
jdbc常见异常及错误解决办法汇总
Solving typeerror: Unicode objects must be encoded before hashing
Qtcreator formatting code
发送邮件工具类
c_ uart_ interface_ Example and offboard modes
Uniapp -- the use of document collation and push of unipush
Go language escape analysis complete record
Uniapp - call payment function: Alipay
SSL/TLS、对称加密和非对称加密和TLSv1.3
如何进行流程创新,以最经济的方式提升产品体验?
Px4 simulation basis
Leetcode (605) -- flower planting
Ad20 learning notes I
Use of xinchida ble 5.0 Low Power Bluetooth module (at command serial port transparent transmission) rsbrs02abr
QLabel 文字水平滚动显示