[Gvsig_english] How to open, modify and edit a dbasefile ?

Matthieu Voorons matthieu.voorons at gmail.com
Wed May 16 21:56:07 CEST 2007


Hi list,
I'm trying to develop a new extension for gvsig. I wish to open the dbf 
file related to a shapefile, modify it (add some fields and change some 
entries) and make some sql queries. How could I do that ?
I've seen that there is a dbf driver in the package 
com.iver.cit.gvsig.fmap.drivers.dbf, and a dbf reader in the package 
com.iver.cit.gvsig.fmap.drivers.shp. Which one should I use ?
I've played a little with the code, the DbaseFileNIO and 
DbaseFileHeaderNIO.
Reading the header is not a problem, but I'm not able to acces the data 
when using DbaseFileReaderNIO :

        String filename = "C:\\properties.dbf";
        File f = new File(filename);
        try {
            FileChannel in = new FileInputStream(filename).getChannel();
            DbaseFileReaderNIO r = new DbaseFileReaderNIO(in);
            int fields = r.getHeader().getNumFields();
            int recordCount = r.getHeader().getNumRecords();
            System.out.println("fields: " + fields + " recordCount: " + 
recordCount);// This return 0 and 0 even if there are 10                 
                                                                        
                                  // fields and 20 records
            String fieldUniqueValueArr[] = new String[recordCount];
            int j = 0;
            while (r.hasNext()) {
                DbaseFileReaderNIO.Row row = r.readRow();
                System.out.println(row.read(0).toString());
                fieldUniqueValueArr[j] = row.read(0).toString();
                j++;
           }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

I'm also wondering how to make a SQL query on the dbf data.
I would be very grateful if you could send me some code snippets or some 
hints to where to digg.

Best regards,
Matthieu Voorons


More information about the Gvsig_internacional mailing list