2016-04-26 38 views
-3
package Csv; 

import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.Writer; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 

public class JtoCModified { 
    private static final String COMMA_DELIMITER = ","; 
    private static final String NEW_LINE_SEPARATOR = "\n"; 
    private static final String FILE_HEADER = "OPV,IKW,OKW,OPI,IPI,NA1,BVV,BVI,NA2,NA3,IPV,_IF,_OF,NA4,SERIAL,NA5,NA6,STATUS,Date,Time,Device_id"; 
    private static final String path ="D:\\Data1.csv"; 
    private static final String A001 = null; 
    String Device_id = A001; 
    private static final int RECORD_COUNT = 20; // * 1000; 
    // static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss"; 
    static final String DATE_FORMAT = "yyyy-MM-dd"; 
    static final String TIME_FORMAT = "HH:mm:ss"; 
    private static final int ADD_MINUTES = 2; 
    static final String FromDate = "2016-01-01 00:00:00"; 



    public static void main(String[] args) throws Exception { 
     File file = new File(path); 
     if (file.exists()) { 
      file.delete(); 
     } 

     List<String> records = new ArrayList<String>(); 
     StringBuffer record = new StringBuffer(); 
     DateFormat df = new SimpleDateFormat(DATE_FORMAT); 
     DateFormat d_f = new SimpleDateFormat(DATE_FORMAT); 
     DateFormat tf = new SimpleDateFormat(TIME_FORMAT); 
     Calendar cal = Calendar.getInstance(); 
     cal.setTime(df.parse(FromDate)); 
     for (int i = 1; i <= RECORD_COUNT; i++) { 
      if (i % 100000 == 0) { 
       records = new ArrayList<String>(RECORD_COUNT); 
      } 
      for (int j = 0; j < 100; j++) { 
       int OPV = 230 + j % 15; // 230 - 244 by 1 
       double IKW = 1.3 + j % 17 * 0.1; // 1.3 - 2.9 by 0.1 
       double OKW = 0.01 + j % 49 * 0.01; // 0.01 - 0.49 by 0.01 
       double OPI = 0.05 + j % 105 * 0.01; // 0.05 - 1.09 by 0.01 
       double IPI = 0.00 + j % 8 * 0.01; 
       int NA1 = 000; 
       int BVV = 104 + j % 13; 
       double BVI = 1.3 + j % 15 * 0.8; 
       int NA2 = 000; 
       int NA3 = 000; 
       int IPV = 241 + j % 1; 
       int _IF = 000; 
       int _OF = 000; 
       int NA4 = 000; 
       int SERIAL = 12345; 
       int NA5 = 000; 
       int NA6 = 000; 
       int STATUS = 00 + j % 01; 
/*    record.delete(0, record.length()); 
       record.append(d_f.format(cal.getTime())); 
       record.append("\t"); 
       record.append(tf.format(cal.getTime())); 
       record.append("\t"); 
       record.append("\t"); 

*/    record.delete(0, record.length()); 
       addToBuffer(record, OPV); 
       addToBuffer(record, IKW); 
       addToBuffer(record, OKW); 
       addToBuffer(record, OPI); 
       addToBuffer(record, IPI); 
       addToBuffer(record, NA1); 
       addToBuffer(record, BVV); 
       addToBuffer(record, BVI); 
       addToBuffer(record, NA2); 
       addToBuffer(record, NA3); 
       addToBuffer(record, IPV); 
       addToBuffer(record, _IF); 
       addToBuffer(record, _OF); 
       addToBuffer(record, SERIAL); 
       addToBuffer(record, NA5); 
       addToBuffer(record, NA6); 

       record.append(STATUS); 
       record.append("\t"); 
       record.append("\t"); 

       record.append("\n"); 
       record.append("\t"); 
       record.append("\t"); 
       cal.setTime(df.parse(FromDate)); 

       cal.add(Calendar.MINUTE, ADD_MINUTES); 
       record.append("\t"); 
       record.append(d_f.format(cal.getTime())); 
       record.append("\t"); 
       record.append(tf.format(cal.getTime())); 
       record.append("\t"); 
       record.append("\t"); 


       records.add(record.toString()); 
       // System.out.println(record); 
      } 
     } 
     record.append("\t"); 

     writeRaw(records); 
    } 

    private static void writeRaw(List<String> records) throws IOException { 
     File file = new File(path); 
     try { 
      FileWriter writer = new FileWriter(file, true); 
      writer.append(FILE_HEADER.toString()); 
      writer.append(NEW_LINE_SEPARATOR); 
      boolean alreadyExists = false; 
      if (!alreadyExists) { 
       writer.append(NEW_LINE_SEPARATOR); 
       write(records, writer); 

      } 
      System.out.println("CSV file was created successfully !!!"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
     } 
    } 

    private static void write(List<String> records, FileWriter writer) throws IOException { 
     long start = System.currentTimeMillis(); 
     for (String record : records) { 
      System.out.println(record); 

      writer.append(record); 
     } 
     writer.flush(); 
     writer.close(); 
     long end = System.currentTimeMillis(); 
     System.out.println((end - start)/1000f); 

     System.out.println("records"); 

    } 

    private static void addToBuffer(StringBuffer buffer, Object data) { 

     buffer.append(data); 
     buffer.append(", "); 
    } 

} 

我要添加日期和時間由製表空格分開,使用\t還是我沒有得到所需要的output.Here,我得到的日期和時間一起單獨添加日期和時間(無標籤分離),如2016-01-01 00:02:00。我想分別打印兩個文件,例如:2016-01-01 00:02:00如何用Java

+1

你想說什麼,什麼是所需的輸出? –

+0

@Scary Wombat請檢查編輯部分 – Programmer

+0

,讓你的'\ t'不工作? –

回答

0

我已經運行你的程序,輸出有點奇怪,因爲你的csv的整體格式不正確,但我想你會在稍後處理。

來到你的問題,數據&時間由tab space輸出所需實際分離。由於格式問題,它在excel上顯示不正確。

所以,你可以使用這個:

record.append((d_f.format(cal.getTime()))+"  "+tf.format(cal.getTime())); 

它要單獨顯示日期&時間在不同的列使用:

record.append((d_f.format(cal.getTime()))+", "+tf.format(cal.getTime())); 
+0

Thanx Ani它的工作,但通過這段代碼我的時間並沒有改變,因爲它應該增加2分鐘。 – Programmer