我使用DbaseFileReader和DBaseFileWriter来读取和更新dbf文件中的记录。当我首先读取文件时,打开DBF文件时没有错误,但是在我使用以下方法向DBF写入一些值后,我得到以下错误
我检查了Object []数组中的值类型,值的类型和计数都等于dbf文件字段。
public static boolean updateFeatureAtts(String shpFile, Map<String, Object> attsFeature) throws IOException {
String file = CN.getMapDataFolder() + CN.FOLDER_LAYERS + "/" + shpFile.replace(".shp", ".dbf");
DbaseFileWriter dbaseFileWriter = null;
DbaseFileReader dbaseFileReader = null;
ReadableByteChannel dbfChannel = null;
WritableByteChannel writableByteChannel = null;
FileOutputStream fileOutputStream = null;
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
dbfChannel = Channels.newChannel(inputStream);
dbaseFileReader = new DbaseFileReader(dbfChannel, true, Charset.forName("Windows-1256"));
DbaseFileHeader header = dbaseFileReader.getHeader();
for (Iterator<Map.Entry<String, Object>> iterator = attsFeature.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry<String, Object> entry = iterator.next();
if (entry.getKey().equals("FID")) {
iterator.remove();
}
}
fileOutputStream = new FileOutputStream(file);
writableByteChannel = Channels.newChannel(fileOutputStream);
dbaseFileWriter = new DbaseFileWriter(header, writableByteChannel, Charset.forName("UTF-8"), TimeZone.getDefault());
Object[] vals = attsFeature.values().toArray();
dbaseFileWriter.write(vals);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null)
inputStream.close();
if (fileOutputStream != null)
fileOutputStream.close();
if (dbaseFileReader != null)
dbaseFileReader.close();
if (dbaseFileWriter != null)
dbaseFileWriter.close();
}
return true;
}
这是错误
java.nio.BufferUnderflowException at
java.nio.DirectByteBuffer.get(DirectByteBuffer.java: 164)
at org.geotools.data.shapefile.dbf.DbaseFileReader.read(DbaseFileReader.java: 417)
at org.geotools.data.shapefile.dbf.DbaseFileReader.readRow(DbaseFileReader.java: 314)
at com.edsab.gedat.util.DBaseFileHandler.findRecord(DBaseFileHandler.java: 174)
at com.edsab.gedat.adapter.AttributePagerAdapter.instantiateItem(AttributePagerAdapter.java: 102)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java: 1003)
at android.support.v4.view.ViewPager.populate(ViewPager.java: 1217)
at android.support.v4.view.ViewPager.populate(ViewPager.java: 1085)
at android.support.v4.view.ViewPager$3.run(ViewPager.java: 273)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java: 777)
at android.view.Choreographer.doCallbacks(Choreographer.java: 590)
at android.view.Choreographer.doFrame(Choreographer.java: 559)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java: 763)
at android.os.Handler.handleCallback(Handler.java: 739)
at android.os.Handler.dispatchMessage(Handler.java: 95)
at android.os.Looper.loop(Looper.java: 145)
at android.app.ActivityThread.main(ActivityThread.java: 6918)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 1199)