-
Notifications
You must be signed in to change notification settings - Fork 74
读文件之原始读
quhongwei edited this page Mar 28, 2018
·
1 revision
原始文件读取就是不需要根据协议布局模板和数据定义模板来把数据转换成对象
1.readRow(Class<?> requiredType) 如果文件是分隔符分割的支持读取, requiredType只能是String[].class
2.支持readLine()
3.不支持 readHead, readTail,getSummary()方法
4.用户完全可用jdk的Reader工具, 组件实现完全是为了接口统一
代码示例
String filePath = File.class.getResource("/reader/de/data/data1.txt").getPath();
FileConfig config = new FileConfig(filePath, "/reader/de/template/template1.json",
new StorageConfig("nas"));
config.setType(FileCoreToolContants.RAW_READER);
FileReader fileReader = FileFactory.createReader(config);
try {
Map<String, Object> head = fileReader.readHead(HashMap.class);
Assert.assertEquals(new Long(100), head.get("totalCount"));
Assert.assertEquals(new BigDecimal("300.03"), head.get("totalAmount"));
Assert.fail();
} catch (RdfFileException e) {
Assert.assertEquals(RdfErrorEnum.UNSUPPORTED_OPERATION, e.getErrorEnum());
}
try {
fileReader.readRow(HashMap.class);
Assert.fail();
} catch (RdfFileException e) {
Assert.assertEquals(RdfErrorEnum.ILLEGAL_ARGUMENT, e.getErrorEnum());
}
String[] row = null;
while (null != (row = fileReader.readRow(String[].class))) {
for (String col : row) {
System.out.print(col);
System.out.print("|");
}
System.out.println();
}
fileReader.close();
fileReader = FileFactory.createReader(config);
String line = null;
while (null != (line = fileReader.readLine())) {
System.out.println(line);
}
fileReader.close();