Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Charset issue with new DBF File #110

Open
fabiosebastiano-vertical opened this issue Mar 21, 2023 · 1 comment
Open

Charset issue with new DBF File #110

fabiosebastiano-vertical opened this issue Mar 21, 2023 · 1 comment
Assignees

Comments

@fabiosebastiano-vertical

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
Copy link

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 :)

@albfernandez albfernandez self-assigned this May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants