/
ReadItAll.java
72 lines (58 loc) · 2.29 KB
/
ReadItAll.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package deringo.dbfsample.javadbf;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import com.linuxense.javadbf.DBFRow;
import com.linuxense.javadbf.DBFUtils;
public class ReadItAll {
public static void main(String[] args) throws IOException {
Path path = Paths.get("src/main/resources/sample.dbf");
List<Map<String, String>> allRows = read(Files.newInputStream(path));
allRows.get(0).keySet().forEach(key -> System.out.println(key +" : "+allRows.get(0).get(key)));
}
public static List<Map<String, String>> read(InputStream in) {
List<Map<String, String>> allRows = new ArrayList<>();
DBFReader reader = null;
try {
// create a DBFReader object
reader = new DBFReader( in );
//
//
List<String> allFieldNames = new ArrayList<>();
// get the field count if you want for some reasons like the following
int numberOfFields = reader.getFieldCount();
// use this count to fetch all field information
// if required
for (int i = 0; i < numberOfFields; i++) {
DBFField field = reader.getField(i);
allFieldNames.add(field.getName());
}
// Now, lets us start reading the rows
DBFRow row;
while ((row = reader.nextRow()) != null) {
Map<String, String> aRow = new HashMap<>();
for (String fieldName : allFieldNames) {
aRow.put(fieldName, row.getString(fieldName));
}
allRows.add(aRow);
}
// By now, we have iterated through all of the rows
return allRows;
} catch (DBFException e) {
e.printStackTrace();
return allRows;
}
finally {
DBFUtils.close(reader);
}
}
}