zoukankan      html  css  js  c++  java
  • 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("
    ");
    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     }
  • 相关阅读:
    scrollview嵌套listiview(解决高度问题以及两者滚动冲突问题)
    listview通过onscrollListener实现分页加载
    listview中的checkbox实现全选、反选、删除的功能
    【JavaScript】闭包应用之数据独立
    【JavaScript】闭包应用之数据缓存
    【问题:发现与解决】angularJs指令在dijit控件中的使用
    【转发】:CSS代码重构与优化之路
    写一个简易的富文本
    【html/css】若母div设置了透明度,如何才能使得里面的子div不继承母div的透明度
    【html/css】模态框的实现
  • 原文地址:https://www.cnblogs.com/wwawp66/p/4440941.html
Copyright © 2011-2022 走看看