有时候流明明已经关闭了,但是还是无法删除文件或者文件夹,提示被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 = InputStream();outputStream = new FileOutputStream(new File(targetPathFile));int n = -1;byte b[] = new byte[1024];while ((n = ad(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();}}}
这样的时候,通常可以这样解决:
();//在删除之前调用垃圾回收,这样jvm就不会占用文件了try {Thread.sleep(100);//我这边是这样,如果不休眠一会,依旧无法删除} catch (InterruptedException e) {e.printStackTrace();}boolean dirFile = FileUtils.deleteQuietly(new File(targetDirPath));boolean zipFile = FileUtils.deleteQuietly(new File(targetZipPathFile));
本文发布于:2024-01-27 18:27:09,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17063512271887.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |