List集合写到本地,生成文件

List集合中的数据写到本地

 

主要方法 及 生成文件方法

 1     private boolean PostFlie(String brand, List<Kpi> listKpis) {
 2 
 3         String year = String.valueOf(getCurrentYear());
 4         String monthString = String.format("%02d", getCurrentMonth() - 1);
 5 
 6         String fileName = brand.toUpperCase() + "KMF_";
 7         String endFileName = brand.toUpperCase() + "KMF_";
 8         java.text.DateFormat dateformat = new java.text.SimpleDateFormat("yyyyMMdd_hhmmss");
 9         String dateStr = dateformat.format(new Date());
10 
11         fileName = fileName + dateStr + ".dat";
12         endFileName = endFileName + dateStr + ".end";
13         ConfigLoader configLoader = ConfigLoader.getInstance();
14         String filePath =  configLoader.getProperty(ConfigLoader.TAP_FILE_TO_LOCAL);
15         StringBuffer buffer = new StringBuffer();
16         for (Kpi kpi : listKpis) {
17             buffer.append(kpi.getDealerCBU() + "|" + kpi.getDealerCKD() + "|"
18                     + kpi.getServiceTypeEnum().getDisplayEnName() + "|"
19                     + kpi.getTitleNameEN() + "|" + year + monthString + "|"
20                     + kpi.getPerformance().getValueForReport()[0] + "|" + brand);
21             buffer.append("\r\n");
22         }
23         try {
24             if (createDir(filePath)) {
25                 String dataFile = filePath + fileName;
26                 FileOutputStream out = new FileOutputStream(dataFile);
27                 log.info("postfile to local: __" + fileName + "__at:" + (new Date()).toString());
28                 
29                 byte[] contentInBytes = buffer.toString().getBytes("utf-8");
30                 out.write(contentInBytes);
31                 out.flush();
32                 out.close();
33                 out = null;
34                 buffer = null;
35                 
36                 String endFile = filePath + endFileName;
37                 out = new FileOutputStream(endFile);
38                 log.info("postfile to local: __" + endFileName + "__at:" + (new Date()).toString());
39                 out.close();
40                 out = null;
41             } else {
42                 return false;
43             }
44         } catch (Exception e) {
45             log.error("Exception when post file to local:", e);
46             return false;
47         } 
48         return true;
49     }
 1     private boolean createDir(String filePath) throws IOException {
 2         try {
 3             File file = new File(new String(filePath.getBytes("UTF-8"), "iso-8859-1"));
 4             if(!file.exists()){
 5                 file.mkdir();
 6             }
 7         } catch (Exception e) {
 8             log.error("Exception when savepath to local", e);
 9             return false;
10         }
11         return true;
12     }

 

posted on 2015-04-20 11:05  JoUU  阅读(1144)  评论(0编辑  收藏  举报

导航