java代码
01 | /** |
02 | * 解析DBF文件 |
03 | * @param in |
04 | * @return List<Map<String,String>> columns通过AMF编码之后的字符串 |
05 | * @throws Exception |
06 | */ |
07 | public static String parseDbf2Map(InputStream in) throws Exception { |
08 | |
09 | DBFReader dbfreader = new DBFReader(in); |
10 | dbfreader.setCharactersetName( "GB2312" ); |
11 | |
12 | List<Map<String,String>> columns = new ArrayList<Map<String,String>>(); |
13 | Map<String,String> head = new HashMap<String,String>(); |
14 | //解析DBF表头的信息 |
15 | Map dataType = new HashedMap(); |
16 | for ( int i = 0 ; i < dbfreader.getFieldCount(); i++) { |
17 | DBFField field = dbfreader.getField(i); |
18 | byte type = field.getDataType(); |
19 | int decimal = 0 ; |
20 | int length = 0 ; |
21 | String formatString = "" ; |
22 | if (DBFField.FIELD_TYPE_N == type || DBFField.FIELD_TYPE_F == type){ |
23 | decimal = field.getDecimalCount(); |
24 | length = field.getFieldLength(); |
25 | for ( int j = 0 ; j < (length - decimal); j++) { |
26 | formatString += "#" ; |
27 | } |
28 | if (decimal != 0 ){ |
29 | formatString = formatString + "0." ; |
30 | } |
31 | for ( int jj = 0 ; jj < decimal; jj++) { |
32 | formatString += "0" ; |
33 | } |
34 | dataType.put( "" +i, formatString); |
35 | } |
36 | if (DBFField.FIELD_TYPE_D == type){ |
37 | formatString = "yyyy/MM/dd" ; |
38 | dataType.put( "" +i, formatString); |
39 | } |
40 | |
41 | String name = field.getName(); |
42 | |
43 | head.put( "" +i, name); |
44 | } |
45 | columns.add(head); |
46 | Object[] rowObj = null ; |
47 | int k = 1 ; |
48 | while ((rowObj = dbfreader.nextRecord()) != null ) { |
49 | Map<String,String> data = new HashMap<String,String>(); |
50 | for ( int i = 0 ; i < rowObj.length; i++) { |
51 | Object temp = rowObj[i]; |
52 | String dataTypeFormat = (String)dataType.get( "" +i); |
53 | if (dataTypeFormat != null ){ |
54 | if (dataTypeFormat.indexOf( "yyyy" ) !=- 1 ){ |
55 | SimpleDateFormat format = new SimpleDateFormat(dataTypeFormat); |
56 | temp = format.format((Date)temp); |
57 | } else { |
58 | DecimalFormat format = new DecimalFormat(dataTypeFormat); |
59 | temp = format.format(temp); |
60 | } |
61 | } |
62 | data.put(dbfreader.getField(i).getName(), temp.toString()); |
63 | } |
64 | columns.add(data); |
65 | k++; |
66 | } |
67 | |
68 | //编码: |
69 | AmfSerializer amfSerializer = new AmfSerializer(); |
70 | String str = amfSerializer.toAmf(columns); |
71 | return str; |
72 |