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

Entry Data becoming Misaligned when using Sync Mode to append record(s) to a file #112

Closed dbfAndroidProject closed 1 year ago

dbfAndroidProject commented 1 year ago

When using the following method to append a record to a DBF file, the data becomes misaligned with spaces when added to the file, overflowing to other fields and completely destroying organization.

public void appendDetailArray(Object[] appendArray) { try { DBFWriter writer = new DBFWriter(new File (getExternalStoragePath() + "/sampleDBF.dbf")); writer.addRecord(appendArray); //Append record writer.close(); //Close } catch (Exception e) { e.printStackTrace(); //Stack trace catch }`

Typically what happens is some fields (Strings, I think) have a couple of spaces added to them on the front, which in a 6 char limit field overflows. This somehow pushes data into the next field, causing it to be misaligned as such Intended: [999999],[00001],[12345],[C], etc Bugged: [ 9999],[990000],[1 1234],[E](decoder function errors out because it's not a valid char option for this use case) I've confirmed multiple times that the object array that I push to the writer.addRecord() method is valid and does not contain any spaces or characters that show up during the misalignment. Has anyone else run into this issue?

sumenkov commented 1 year ago
try (DBFWriter writer = new DBFWriter(new FileOutputStream(saveFilePath), Charset.forName(charsetName))){
    writer.setFields(fields);
    for (Object rowData: rowsData.subList(1, rowsData.size())) {
        writer.addRecord((Object[]) rowData);
    }
}
dbfAndroidProject commented 1 year ago

Problem solved, but I didn't use this method. It had to do with a corrupted DBF file, but I'd like to keep this here for others to reference in case it solves their issue. Issue Closed.