1. Prepare:
Jar package download: Link: https://pan.baidu.com/s/1Ikxx-vkw5vSDf9SBUQHBCw extraction code: 7h58 copy the contents of this open Baidu network disk phone App, the operation more convenient oh
2. Case:
import com.linuxense.javadbf.DBFDataType; import com.linuxense.javadbf.DBFField; import com.linuxense.javadbf.DBFReader; import com.linuxense.javadbf.DBFWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 读写DBF文件工具类 */ public class DBFUtils { /** * 读DBF文件 * @param path * @param charsetName * @return * @throws IOException */ public static List<Map<String, String>> readDbf(String path, String charsetName) { List<Map<String, String>> rowList = new ArrayList<>(); DBFReader dbfReader = null; Object[] rowValues; try { dbfReader = new DBFReader(new FileInputStream(path), Charset.forName(charsetName)); while ((rowValues = dbfReader.nextRecord()) != null) { Map<String, String> rowMap = new HashMap<>(); for (int i = 0; i < rowValues.length; i++) { rowMap.put(dbfReader.getField(i).getName(), String.valueOf(rowValues[i]).trim()); } rowList.add(rowMap); } } catch (FileNotFoundException e) { e.printStackTrace(); } the finally { IF (! dbfReader = null ) { dbfReader.close (); } } return rowList; } / ** * Create DBF * @param path: File Path * @param the fieldList: Fields * @param charsetName coded character set * @ throws IOException * / public static void createDbf (String path, List <the Map <String, String >> the fieldList, String charsetName) { DBFField [] Fields = new newDBFField [fieldList.size ()]; int index = 0 ; for (the Map <String, String> FieldMap: the fieldList) { DBFField Field = new new DBFField (); field.setName (fieldMap.get ( "name")); / / field name field.setType (DBFDataType.CHARACTER); // specified field type as a string ; field.setLength (Integer.valueOf (fieldMap.get ( "length"))) // specified length fields [index] = field; index ++ ; } // define dBFWriter examples used to write files DBF dBFWriter dbfWriter =null ; the try { dbfWriter = new new DBFWriter ( new new a FileOutputStream (path), Charset.forName (charsetName)); // set field dbfWriter.setFields (Fields); } the catch (a FileNotFoundException E) { e.printStackTrace (); } the finally { // write the file and close dbf IF (dbfWriter =! null ) { dbfWriter.close (); } } } / ** * Get field name * @param path * @param charsetName * @return * @throws IOException */ public static String[] getFieldName(String path, String charsetName){ DBFReader dbfReader = null; String[] fieldName = null; try { dbfReader = new DBFReader(new FileInputStream(path), Charset.forName(charsetName)); int fieldCount = dbfReader.getFieldCount();//获取字段数量 fieldName = new String[fieldCount]; for (int i = 0; i < fieldCount; i++) { fieldName[i] = dbfReader.getField(i).getName(); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (dbfReader != null){ dbfReader.close(); } } return fieldName; } /** * 写dbf文件 * @parampath: dbf file path * @param rowList: rows to be written * @param charsetName: Character Set * @throws IOException * / public static void writeDbf (String path, List <the Map <String, String >> rowList, String charsetName ) { DBFWriter dbfWriter = new new DBFWriter ( new new File (path)); // Get field String [] = the fieldName getFieldName (path, "GBK" ); for (the Map <String, String> rowMap: rowList) { Object [], rowData = new newObject [fieldName.length]; for ( int I = 0; I <rowData.length; I ++ ) { // The field means are arranged, where dislocation may occur otherwise , rowData [I] = rowMap.get (the fieldName [I]) ; } // add a record (and not written to the file at this time) dbfWriter.addRecord (, rowData); } // write dbf file and save close dbfWriter.close (); } public static void main (String [] args) { List <the Map <String, String >> MAPLIST = new new the ArrayList <> (); the Map <String, String> Map = new new HashMap<>(); map.put("name","A10"); map.put("length","18"); mapList.add(map); //创建文件 createDbf("src\\demo\\knowledgepoints\\dbf\\1.DBF",mapList,"GBK"); mapList.clear(); map = new HashMap<>(); map.put("A10","蕾蕾"); mapList.add(map); //写文件 writeDbf("src\\demo\\knowledgepoints\\dbf\\1.DBF",mapList,"GBK"); //读取 mapList = readDbf("src\\demo\\knowledgepoints\\dbf\\1.DBF","GBK"); System.out.println(mapList); } }
operation result:
Files are also corresponding position:
Reference: https://blog.csdn.net/u010689849/article/details/90340745