albfernandez / javadbf

Java library for reading and writing Xbase (dBase/DBF) files.
GNU Lesser General Public License v3.0
220 stars 98 forks source link

Charset issue with new DBF File #110

Open fabiosebastiano-vertical opened 1 year ago

fabiosebastiano-vertical commented 1 year ago

Hi, I'm using this library to change the column size/type of some DBF created with GEOTOOL package. The original version of the DBF files works fine, but when I create a new DBF from the scratch, add the headers and the fields read from the original file (that are in ISO-8859-1), no matter what i do, the final files have a different (windows-1252) charset.

As you can see in the following snippet, I'm passing the charset to the reader and to the writer and, if i print out the charset with the proper method, it seems properly set (ISO-8859-1), but If i open the shape file in QGIS, it says that it has the "windows-1252" charset.

`

    DBFReader reader    = null;
    DBFField[] fields   = null; 
    File originalDBF    = new File("original_file.dbf");
    File newDBF         = new File("final_file.dbf");
    try {
        FileInputStream fis = new FileInputStream(originalDBF);
        FileOutputStream fos = new FileOutputStream(newDBF);
        reader              = new DBFReader(fis,Charset.forName("ISO-8859-1"));
        DBFWriter writer    = new DBFWriter(fos, Charset.forName("ISO-8859-1"));

        int numberOfFields  = reader.getFieldCount();
        fields              = new DBFField[numberOfFields];

        for (int i = 0; i < numberOfFields; i++) {
            DBFField originalField = reader.getField(i);
            fields[i] = new DBFField();
            fields[i].setName(originalField.getName());
            fields[i].setType(originalField.getType());

            //DOING SOMETHING WITH THE FIELDS HEADER
        }
        writer.setFields(fields);

       Object[] rowObjects;
       Object rowData[] = new Object[numberOfFields];
        while ((rowObjects = reader.nextRecord()) != null) {
            for (int i = 0; i < rowObjects.length; i++) {
              //DOING SOMETHING WITH THE ROWS
            }
            writer.addRecord(rowData);
        } 
        writer.close();
    } catch (DBFException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        DBFUtils.close(reader);
    }

`

Could you please help me?

sumenkov commented 1 year ago

Hello, Everything seems to be correct. I want to slightly correct the entry in the dbf file:

String newDBF = "final_file.dbf";
Object[] rowObjects = reader.nextRecord();
Object rowData[] = new Object[numberOfFields];

try (DBFWriter entry = new DBFWriter(new FileOutputStream(new DBF), Charset.forName("iso-8859-1"))) {

    writer.setFields(fields);

    while (rowObjects != null) {
        for (int i = 0; i < rowObjects.length; i++) {
            // DO SOMETHING WITH THE STRINGS
        }
        writer.addRecord(rowData);
    }
}

I also read the incoming dbf file a little differently, you can see it in my repository: https://github.com/sumenkov/DBFandExcel

If you give a link to the original dbf file, I'll try to rewrite and check the encoding status :)