如何增加文件名和压缩输出文件

时间:2016-09-09 04:16:13

标签: java csv zip

我想增加我的CSV文件名csv1,csv2的输出我还要Zip创建的CSV文件命名约定应该像zip1和zip2一样增加zip1的内容是csv1而zip2是csv2 < / p>

   public void  process(Variables variables) throws SQLException, IOException{//CUST ORDER FILE
try{
  log.info("Processing table");
          connection connect = new connection();
          connect.setConnection(variables);
           if (variables.conn!= null){
               Statement stmt = variables.conn.createStatement();
               String query="SELECT (SELECT SBS FROM STOREDEF WHERE STOREID=4) AS BRANCH, OD.DT_DATE as TRANS_DATE, OC.ORDER_CODE,   \n" +
              "OD.ORDER_BARCODE,OC.FIRST_NAME,OC.LAST_NAME,OC.ADDRESS,OC.CITY,OC.DISTRICT,OC.PHONE,OC.STATE, '' as DEL_DATE  \n" +
             ",OH.AMOUNT_ALREADY_PAID, OD.DT_TIME, \n" +
             "CASE OD.DEPTNUM   \n" +
                 "WHEN '90003' THEN '1'   \n" +
                 "WHEN '90099' THEN '2'   \n" +
                 "END AS ORDER_TYPE,TO.TID,TO.XACTNBR,'' AS ITEM_ORIGIN,OC.CUSTOMER_CODE AS ZONE_FIELD \n" +
                 "FROM ORDERS_CUSTOMERS OC   \n" +
                 "LEFT JOIN ORDERS_DETAILS OD   \n" +
                 "ON OC.ORDER_CODE = OD.ORDER_CODE   \n" +
                 "LEFT JOIN ORDERS_HEADER OH   \n" +
                 "ON OC.ORDER_CODE=OH.ORDER_CODE   \n" +
                 "LEFT JOIN TL_ORDERS TO   \n" +
                 "ON OC.ORDER_CODE=TO.ORDERCODE   \n" +
                 "WHERE  OD.DT_DATE = '2016-07-19' AND OD.DT_TIME BETWEEN '10:00:00' AND '23:00:00'";
               ResultSet myRs = stmt.executeQuery(query);
               log.info("Query executed");
               if(myRs.next()){
                   String BRANCH =myRs.getString("BRANCH");
                   String TRANS_DATE = myRs.getString("TRANS_DATE");  
                   String ORDER_CODE = myRs.getString("ORDER_CODE");
                   String ORDER_BARCODE = myRs.getString("ORDER_BARCODE");
                   String FIRST_NAME = myRs.getString("FIRST_NAME"); 
                   String LAST_NAME = myRs.getString("LAST_NAME"); 
                   String ADDRESS = myRs.getString("ADDRESS"); 
                   String CITY = myRs.getString("CITY"); 
                   String DISTRICT = myRs.getString("DISTRICT");
                   String PHONE = myRs.getString("PHONE");
                   String STATE = myRs.getString("STATE");
                   String DEL_DATE =myRs.getString("DEL_DATE");
                   String AMOUNT_ALREADY_PAID = myRs.getString("AMOUNT_ALREADY_PAID");
                   String ORDER_TYPE = myRs.getString("ORDER_TYPE");
                   String TID = myRs.getString("TID");
                   String XACTNBR  = myRs.getString("XACTNBR");
                   String ITEM_ORIGIN = myRs.getString("ITEM_ORIGIN");
                   String ZONE_FIELD = myRs.getString("ZONE_FIELD");

               }

             Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ;
       String CSVFile = "C:\\Vstore\\OurHome\\sales\\CUSTOURHOMEMEGAMALL123"+dateFormat.format(date)+".CSV";
       CSVWriter writer = new CSVWriter(new FileWriter(CSVFile),
               '|',
               CSVWriter.NO_QUOTE_CHARACTER,
               CSVWriter.NO_ESCAPE_CHARACTER,
               "\r\n");
         String[] header = {"BRANCH"+
               "|TRANS_DATE"+
               "|ORDER_CODE"+
               "|ORDER_BARCODE"+
               "|FIRST_NAME"+
               "|LAST_NAME"+
               "|ADDRESS"+
               "|CITY"+
               "|DISTRICT"+
               "|PHONE"+
               "|STATE"+
               "|DEL_DATE"+
               "|AMOUNT_ALREADY_PAID"+
               "|ORDER_TYPE"+
               "|TID"+
               "|XACTNBR"+
               "|ITEM_ORIGIN"+
               "|CUSTOMER_CODE"};
       writer.writeNext(header);
       writer.writeAll(myRs,false);
       log.info("csv created");


       writer.close();
       myRs.close();
       stmt.close();
           }
        variables.conn.close();
        log.info("done processing");

           } catch(Exception e){
              log.error("Error in processing");
    }
}

1 个答案:

答案 0 :(得分:0)

文件获取您惊讶的传递给FileWriter()构造函数的名称。

所以你唯一要做的就是:

String numberedCsvFile = csvFileName +&#34; 1&#34;;

例如。

当然,真正的诀窍是:跟踪已经创建的csv文件。有两种方法可以实现:

  1. 如果所有文件都是由程序的实例创建的,那么您只需保留一个内部计数器(在编写新的CSV文件后将其递增)
  2. 否则,您的程序必须检查&#34;输出&#34;的内容。目录;看看有多少&#34; .csvN&#34;文件已经存在。因此,当它发现.csv1和.csv2存在时,下一个要创建的将是.csv3
  3. 要了解如何在java中创建ZIP文件,您可以开始阅读here

    一些注意事项:

    1. 阅读有关Java命名约定的内容。变量总是来自camelCase,即使它们被称为CSVFile。
    2. 我建议不要命名.csv1,csv2,...但是&#34; file-1.csv&#34;,&#34; file-2.csv&#34;等等。文件扩展名有一定的含义;你不应该腐败&#34;没有真正需要这样做。