0

I want to increment the output of my CSV File name csv1,csv2 I also want to Zip the CSV File that was created the naming convention should be increment too like zip1 and zip2 the content of zip1 is csv1 and zip2 is csv2

   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");
    }
}
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89

1 Answers1

0

Files get the names that you, surprise, pass to the FileWriter() constructor.

So the only thing you have to do:

String numberedCsvFile = csvFileName + "1";

for example.

Of course, the real trick here is: keeping track of the already created csv files. There are two ways to get there:

  1. If all files are created by the same instance of your program, then you just keep an internal counter (you increment it after you wrote a new CSV file)
  2. Otherwise, your program has to check the contents of your "output" directory; and see how many ".csvN" files are already out there. So when it finds that .csv1 and .csv2 exist, the next one to create would be .csv3

To learn how to create ZIP files in java, you could start reading here.

Some notes:

  1. read about Java naming conventions. Variables go camelCase always, even when they are called CSVFile.
  2. I suggest to not name .csv1, csv2, ... but "file-1.csv", "file-2.csv" and so on. File name extensions have a certain meaning; and you shouldn't "corrupt" that without a real need to do so.
Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I am using a schedule task where my program will run every hour and how will i add a internal counter?. Also in creating zip file i want to automatically zip the file that will be generated, – Manuel Perez Sep 09 '16 at 05:19
  • My answer **answers** your questions. I gave you exactly the things you need to implement that. Yourself. Or do you expect that other people to **your** work for you? – GhostCat Sep 09 '16 at 05:22