/** * dbf文件 */ public static final String DBF_FILE = "/Users/guanxianghui/Desktop/user.dbf"; /** * 测试读取文件-逐条记录读取,根据列名读取列 */ @Test public void testRead2() { System.out.println("[测试读取文件-逐条记录读取,根据列名读取列]"); System.out.println("DBF文件路径:[" + DBF_FILE + "]"); DBFReader reader = null; try { /** * 读工具类,指定文件 */ reader = new DBFReader(new FileInputStream(DBF_FILE), Charset.forName("GBK")); /** * 读取表结构 */ int numberOfFields = reader.getFieldCount(); System.out.println("表结构共[:" + numberOfFields + "]列"); System.out.println("========="); for (int i = 0; i < numberOfFields; i++) { DBFField field = reader.getField(i); System.out.println("列:" + field.getName() + "[" + field.getLength() + "]"); } System.out.println("========="); System.out.println("总记录数[" + reader.getRecordCount() + "],逐条记录读取,根据列名读取列"); System.out.println("========="); /** * 逐条记录读取,根据列名读取列 */ DBFRow row; int index = 0; while ((row = reader.nextRow()) != null) { System.out.println("第[" + (++index) + "]记录,COUNTRY:[" + row.getString("COUNTRY") + "],NAME:[" + row.getString("NAME") + "],SALARY:[" + row.getString("SALARY") + "]"); } System.out.println("========="); } catch (DBFException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { /** * 关闭资源 */ if (null != reader) { DBFUtils.close(reader); } } }