Skip to content

写文件之正常写

quhongwei edited this page Mar 28, 2018 · 1 revision

一:数据定义模板

{
	"head":[
        "identity|信息标识|[8,0]|default:OFDCFDAT",
        "version|协议版本号|[4,0]|default:20",
        "msgCreator|信息创建人|[9,0]|default:H0",
        "msgRecipient|信息接收人|[9,0]",
        "sendDate|传送发生日期|[8,0]|Date:yyyyMMdd",
        "summaryTableNo|汇总表号|[3,0]",
        "fileTypeCode|文件类型代码 |[2,0]",
        "sender|发送人|[8,0]|default:H0",
        "recipient|接收人|[8,0]"
    ],
    "body":[
        "TransactionCfmDate|对帐日期|[8,0]|Date:yyyyMMdd",
        "FundCode|基金代码|[8,0]",
        "AvailableVol|基金可用份数|Integer|[6,2]"
    ],
    "tail":[
    	"fileEnd|数据文件尾部字符|default:OFDCFEND|[8,0]"
    ],
    "protocol":"FUND"
}

二:协议布局模板

fund组件内置协议

三: 代码示例

FileConfig config = new FileConfig(new File(filePath, "test.txt").getAbsolutePath(), "/writer/template/fund.cfg", new StorageConfig("nas"));
FileWriter fileWriter = FileFactory.createWriter(config);
try {            
  //构建文件头
        Map<String, Object> head = new HashMap<String, Object>();
        head.put("msgRecipient", "xxx");
        head.put("sendDate", DateUtil.parse("20151204", "yyyyMMdd"));
        head.put("summaryTableNo", "aa");
        head.put("fileTypeCode", "bb");
        head.put("recipient", "ll");
        head.put("totalCount", 1); //总笔数必须要知道
        fileWriter.writeHead(head);
        // 文件数据内容
        Map<String, Object> row = new HashMap<String, Object>();
        row.put("TransactionCfmDate", DateUtil.parse("20151204", "yyyyMMdd"));
        row.put("FundCode", "中国1");
        row.put("AvailableVol", 42.11);
        fileWriter.writeRow(row);
        // 文件尾,没有数据,是取了数据定义模板中默认值
        fileWriter.writeTail(new HashMap<String, Object>());
}finally {
        fileWriter.close();
}

四:生成的文件示例

OFDCFDAT
20  
H0       
xxx      
20151204
aa 
bb
H0      
ll      
003
TransactionCfmDate
FundCode
AvailableVol
00000001
20151204中国1 004211
OFDCFEND
Clone this wiki locally