jdbf库是一个读写dbf文件的能用的java库,对于一般的dbf文件,它都能很好的处理,不依赖于操作系统。
以前曾经在http://code.google.com/p/java-dbf上发布了一个,但是整理得不太好。源码文件的字符集设置不合理,导致非中文用户打不开文件或者编译不成功。
这次统一迁移到了http://code.google.com/p/jdbf上,源码和库统一存放,有兴趣的可以去下载。
这次统一进行了整理,使用UTF-8不带BOM编码。使用简单的ant进行编译。非常直观。
简单的使用示例:
e.g. Write a DBF:
public static void main(String args[]) throws Exception { JDBField[] fields = { new JDBField("ID", 'C', 8, 0), new JDBField("Name", 'C', 32, 0), new JDBField("TestN", 'N', 20, 0), //第三个参数值一定不大于20 new JDBField("TestF", 'F', 20, 6), //F类型与N类型同,且第四个参数值有小数位数,否则会截短 new JDBField("TestD", 'D', 8, 0) }; //DBFReader dbfreader = new DBFReader("E:\\hexiong\\work\\project\\book2.dbf"); DBFWriter dbfwriter = new DBFWriter("./testwrite.dbf", fields); Object[][] records = { {"1", "hexiong", new Integer(500), new Double(500.123), new Date() }, {"2", "hefang", new Integer(600), new Double(600.234), new Date() }, {"3", "heqiang", new Integer(700), new Double(700.456), new Date() } }; for (int i=0; i<records.length; i++){ dbfwriter.addRecord(records[i]); } dbfwriter.close(); System.out.println("testwrite.dbf write finished......."); }
e.g. Read a dbf:
public static void main(String args[]) throws Exception { //DBFReader dbfreader = new DBFReader((new URL("http://www.svcon.com/us48st.dbf")).openStream()); //DBFReader dbfreader = new DBFReader("F:\\work\\book2.dbf"); DBFReader dbfreader = new DBFReader("./book2.dbf"); //DBFReader dbfreader = new DBFReader("E:\\hexiongshare\\test.dbf"); int i; for (i=0; i<dbfreader.getFieldCount(); i++) { System.out.print(dbfreader.getField(i).getName()+" "); } System.out.print("\n"); for(i = 0; dbfreader.hasNextRecord(); i++) { Object aobj[] = dbfreader.nextRecord(Charset.forName("GBK")); for (int j=0; j<aobj.length; j++) System.out.print(aobj[j]+" | "); System.out.print("\n"); } System.out.println("Total Count: " + i); }