B4J Code Snippet [DBF] Read/Write DBF file using jDBF

AnandGupta

Expert
Licensed User
Longtime User
Another point, how to close the opened dbf after read ?
Now it stays shared opened.

Got it.
 
Last edited:

AnandGupta

Expert
Licensed User
Longtime User
Hi @aeric

I have another problem which I could not fix. See if you can give me a hand here.

Our clients enter string data with some leading spaces so that it shows above in sorted list.
Now jDBF trims all spaces both left and right, so I get the data without any leading spaces.
The right trim is Ok for me, but left trim is problem, as I need to show the list as it shows in my XBase++ application.

I changed " INVESTMENTS" value to have 2 leading spaces, in book2.dbf (attached),
You can see the logs as in screen shots attached, also below text

B4X:
11
B  |  I  |  03  |  CURRENT LIABILITIES & PROVISIONS  |  02  |  Provisions  |  03  |  02  |    |    |  false  |    |  
12
B  |  O  |  04  |  FIXED ASSETS  |    |    |  01  |    |    |    |  false  |    |  
13
B  |  O  |  04  |  FIXED ASSETS  |  01  |  Net Block  |  01  |  01  |    |    |  false  |    |  
14
B  |  O  |  04  |  FIXED ASSETS  |  02  |  Capital Work In Progress  |  01  |  02  |    |    |  false  |    |  
15
B  |  O  |  05  |  INVESTMENTS  |    |    |  02  |    |    |    |  false  |    |  
16
B  |  O  |  06  |  CURRENT ASSETS, LOANS & ADVANCES  |    |    |  03  |  01  |    |    |  false  |    |  
17
B  |  O  |  06  |  CURRENT ASSETS, LOANS & ADVANCES  |  01  |  Inventories  |  03  |  02  |    |    |  false  |    |  
18
B  |  O  |  06  |  CURRENT ASSETS, LOANS & ADVANCES  |  02  |  Sundry Debtors  |  03  |  03  |    |    |  false  |    |  
19
B  |  O  |  06  |  CURRENT ASSETS, LOANS & ADVANCES  |  03  |  Cash & Bank Balances  |  03  |  04  |    |    |  false  |    |  
20
B  |  O  |  06  |  CURRENT ASSETS, LOANS & ADVANCES  |  04  |  Other Current Assets  |  03  |  05  |    |    |  false  |    |  
21
B  |  O  |  06  |  CURRENT ASSETS, LOANS & ADVANCES  |  05  |  Loans & Advances  |  03  |  06  |    |    |  false  |    |

The "INVESTMENTS" do not have the leading spaces.

I studied the java code of the jDBF source but do not understand how to fix / change it, as I have limited Java knowledge.
I hope you can guide me here.
 

Attachments

  • book2.zip
    854 bytes · Views: 155
  • ba2.png
    21.6 KB · Views: 150
  • ba1.png
    26.8 KB · Views: 150

aeric

Expert
Licensed User
Longtime User
I think I found the Java code that trim the String. I try to recompile the library again.
 

AnandGupta

Expert
Licensed User
Longtime User
Yes! Now both leading and trailing spaces are printing.
Thanks a lot.

The trim() code is in line #225 of JDBField.java
Ahh..I also see it now after your direction.

Can I compile the sources ? Say if I want to play with it more. I do not know how to do it.
 

aeric

Expert
Licensed User
Longtime User
Yes! Now both leading and trailing spaces are printing.
Thanks a lot.


Ahh..I also see it now after your direction.

Can I compile the sources ? Say if I want to play with it more. I do not know how to do it.
Yes, you can recompile the source on your own.
I use IntelliJ and Build artifact to output the jar file. I will upload my modified version later.
 

AnandGupta

Expert
Licensed User
Longtime User
Now with 1.4 jar I am getting below error in one of my dbf, (it is Ok in 1.3)
B4X:
>CLASS<  |  >CDES<  |  >TAG<  |  >TYPE<  |  >ORDER<  |  >ASSNO<  |  >TFTYPE<  |  >CHAPTER<  |  >CTSH<  |  >S_CAT<  |  >STATE_ID<  |  >CATEGORY<  |  >YS_ACEDNS<  |  >Y_AMIT<  |  
main._appstart (java line: 87)
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at b4j.example.main._appstart(main.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
    at b4j.example.main.main(main.java:28)
Caused by: com.hexiong.jdbf.JDBFException: For input string: "  1"
    at com.hexiong.jdbf.JDBField.parse(JDBField.java:251)
    at com.hexiong.jdbf.DBFReader.nextRecord(DBFReader.java:249)
    ... 13 more

I have attached the dbf, named as book2. I do not think removal of .trim() is causing it, but you can understand the java error.
 

Attachments

  • book2.zip
    3.5 KB · Views: 160

aeric

Expert
Licensed User
Longtime User
Caused by: com.hexiong.jdbf.JDBFException: For input string: " 1"
Yes, it is caused by removal of trim().
Need to know which row and field type to fix it.

Edit: Found the bug. Uploaded jdbf-1.5.jar in post #1.

JDBField.java:
    public Object parse(String s) throws JDBFException {
        // s = s.trim();
        if (type == 'N' || type == 'F') {
            s = s.trim(); // <-- Moved to here
            if (s.equals("")) {
                s = "0";
            }
            try {
                if (getDecimalCount() == 0) {
                    return new Long(s);
                } else {
                    return new Double(s);
                }
            } catch (NumberFormatException numberformatexception) {
                throw new JDBFException(numberformatexception);
            }
        }
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
The problem is that Java’s trim is an all or nothing trim when it comes to removing white spaces from the front AND rear of a string. DBF expects only the removal of trailing spaces. Here is a link to a solution to the issue (and it depends on Java version used to compile the library).

 

AnandGupta

Expert
Licensed User
Longtime User
Now we also need to .trim() date as if blank it gives below error, (I now learnt how to check the error, from your hints )
B4X:
>CNAME<  |  >CADD1<  |  >BLEN<  |  >TLEN<  |  >DT1<  |  >DT2<  |  >INDIVIDUAL<  |  >PRET<  |  >SRET<  |  
main._appstart (java line: 87)
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at b4j.example.main._appstart(main.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
    at b4j.example.main.main(main.java:28)
Caused by: com.hexiong.jdbf.JDBFException: java.text.ParseException: Unparseable date: "        " value=[        ]
    at com.hexiong.jdbf.JDBField.parse(JDBField.java:269)
    at com.hexiong.jdbf.DBFReader.nextRecord(DBFReader.java:249)
    ... 13 more

Attached 'book2.dbf' modified with blank date.
 

Attachments

  • BOOK2.ZIP
    286 bytes · Views: 143

AnandGupta

Expert
Licensed User
Longtime User
My problem is not removing the spaces, which I can do in B4X itself. Problem is to get string value as it is, i.e. both leading and trailing spaces.
jDBF as modified by Aeric now does it.
 

aeric

Expert
Licensed User
Longtime User
Updated version 1.6 in post #1.

Edit: Updated as version 1.3 in post #1.
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…