为什么使用geotools读取shapefile的代码会抛出这个异常?

7
我正在使用Geotools 10.1从Shapefile中读取属性。在打印所有要素属性后,我不明白为什么会抛出异常。
以下是示例代码:
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.simple.SimpleFeature;

public class LayerBusinessTest {


public static void main(String[] args) throws IOException {

    File file = new File("../../setup/test/shp/sscc/SSCC2010_WGS84.shp");
    Map<String, Serializable> map = new HashMap<>();
    map.put( "url", file.toURI().toURL() );

    DataStore dataStore = DataStoreFinder.getDataStore( map );
    String typeName = dataStore.getTypeNames()[0];

    FeatureSource source = dataStore.getFeatureSource( typeName );

    FeatureCollection collection =  source.getFeatures();
    FeatureIterator<SimpleFeature> results = collection.features();
    try {
        while (results.hasNext()) {
            SimpleFeature feature = (SimpleFeature) results.next();
            String code = feature.getAttribute("Codigo_SSC").toString();
            System.out.println( code );
        }
    } finally {
        results.close();
    }

}

}

异常:

Exception in thread "main" java.lang.IllegalArgumentException: Expected requestor org.geotools.data.shapefile.dbf.DbaseFileReader@2ac9b619 to have locked the url but it does not hold the lock for the URL
    at org.geotools.data.shapefile.files.ShpFiles.unlockRead(ShpFiles.java:429)
    at org.geotools.data.shapefile.files.FileChannelDecorator.implCloseChannel(FileChannelDecorator.java:149)
    at java.nio.channels.spi.AbstractInterruptibleChannel.close(AbstractInterruptibleChannel.java:115)
    at org.geotools.data.shapefile.dbf.DbaseFileReader.close(DbaseFileReader.java:279)
    at org.geotools.data.shapefile.ShapefileFeatureReader.close(ShapefileFeatureReader.java:248)
    at org.geotools.data.store.ContentFeatureCollection$WrappingFeatureIterator.close(ContentFeatureCollection.java:154)
    at LayerBusinessTest.main(LayerBusinessTest.java:39)

我也遇到了这个错误。你是怎么解决的? - shorif2000
@sharif 请阅读被标记为正确的答案。 - angelcervera
1个回答

13

在退出之前必须执行dataStore.dispose();


每当我像这样访问一个shapefile时,我都会添加一个finally块来调用dispose(),即使你正在使用Java 7+的try with resources。 - Mark Giaconia
很有趣,因为我之前使用的是旧版本(15.1),这个操作并不需要。但当我切换到20.0版本后,我开始遇到OP的错误。感谢angelcervera! - T Sloane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接