首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我不能在释放资源后删除tif文件?

为什么我不能在释放资源后删除tif文件?
EN

Stack Overflow用户
提问于 2022-01-14 02:02:18
回答 1查看 119关注 0票数 1

我想阅读这个tiff的bbox,完成后我想删除这个tiff,似乎有一个未关闭的流导致tiff不被删除。

代码语言:javascript
复制
public static void main(String[] args) throws Exception {
        File file = new File("E:\\test\\1\\昂多.tif");
        GeoTiffReader reader = new GeoTiffReader(file);
        GridCoverage2D read = reader.read(null);
        Envelope2D coverageEnvelope = read.getEnvelope2D();
        Rectangle2D bounds2D = coverageEnvelope.getBounds2D();
        double[] bbox = {
                bounds2D.getMinX(), bounds2D.getMinY(),
                bounds2D.getMaxX(), bounds2D.getMaxY()
        };
        for (double v : bbox) {
            System.out.println(v);
        }
        read.dispose(true);
        reader.dispose();
        FileUtils.forceDelete(file);
    }

代码运行结果

代码语言:javascript
复制
98.408460773
30.078745248
98.460743439
30.115231446
Exception in thread "main" java.io.IOException: Unable to delete file: E:\test\1\昂多.tif
    at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:2400)
    at cn.gisdata.core.publish.strategyImpl.Test.main(Test.java:37)

Process finished with exit code 1

GeoTools GridCoverage2D dispose(布尔力) api doc

代码语言:javascript
复制
public boolean dispose(boolean force)
Provides a hint that a coverage will no longer be accessed from a reference in user space. This method disposes the image only if at least one of the following conditions is true (otherwise this method do nothing):
force is true, or
The underlying image has no sinks.
This safety check helps to prevent the disposal of an image that still used in a JAI operation chain. It doesn't prevent the disposal in every cases however. When unsure about whatever a coverage is still in use or not, it is safer to not invoke this method and rely on the garbage collector instead.

Overrides:
dispose in class AbstractCoverage
Parameters:
force - true for forcing an inconditionnal disposal, or false for performing a conservative disposal. The recommanded value is false.
Returns:
true if this method disposed at least some resources, or false if this method vetoed against the disposal.
Since:
2.4
See Also:
PlanarImage.dispose()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-25 07:56:33

修改代码

代码语言:javascript
复制
public static void main(String[] args) throws Exception {
        File file = new File("E:\\test\\1\\昂多.tif");
        GridCoverage2D read = null;
        GeoTiffReader reader = null;
        try (FileInputStream is = new FileInputStream(file)){
            reader = new GeoTiffReader(is);
            read = reader.read(null);
            Envelope2D coverageEnvelope = read.getEnvelope2D();
            Rectangle2D bounds2D = coverageEnvelope.getBounds2D();
            double[] bbox = {
                    bounds2D.getMinX(), bounds2D.getMinY(),
                    bounds2D.getMaxX(), bounds2D.getMaxY()
            };
            for (double v : bbox) {
                System.out.println(v);
            }
        }finally {
            read.dispose(true);
            reader.dispose();
            FileUtils.forceDelete(file);
        }

    }

代码运行结果

代码语言:javascript
复制
98.408460773
30.078745248
98.460743439
30.115231446

Process finished with exit code 0

GeoTiffReader构造方法接受inputStream,因此这个输入流由我控制以关闭。

代码语言:javascript
复制
if (this.source instanceof InputStream || this.source instanceof ImageInputStream) {
                this.closeMe = false;
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70705253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档