2013-11-09 22 views
5

Czytam atrybuty z pliku kształtu za pomocą geotools 10.1. Nie rozumiem, dlaczego zgłasza wyjątek po wydrukowaniu wszystkich atrybutów funkcji.Dlaczego ten kod jak odczytać plik kształtu za pomocą geotools zgłasza ten wyjątek?

To jest przykładowy kod:

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(); 
    } 

} 

}

Wyjątek:

Exception in thread "main" java.lang.IllegalArgumentException: Expected requestor org.geotools.data.shapefile.dbf.DbaseFileR[email protected] 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) 
+0

Otrzymuję ten sam błąd. jak rozwiązujesz? ' – shorif2000

+0

@sharif Przeczytaj odpowiedź oznaczoną jako poprawna. – angelcervera

Odpowiedz

9

Jest to konieczne, wykonać dataStore.dispose(); przed wyjściem.

+0

do dodania, za każdym razem, gdy uzyskuję dostęp do pliku shapefile w ten sposób, dodam blok w końcu do wywołania dispose(), nawet jeśli używasz Java 7+ spróbuj z zasobami. – markg