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
仍然没有得到所需的输出。这里,我得到了日期和时间(没有标签分隔),如{{1 }}。我想单独打印,例如:2016-01-01 00:02:00
。
答案 0 :(得分:0)
我已经运行了你的程序并且输出有点奇怪,因为你的整体csv格式不对,但我想你以后会处理它。
来到你的问题,数据&amp;时间实际上按要求在输出中用tab space
分隔。由于格式化问题,它在excel上无法正常显示。
所以你可以用这个:
record.append((d_f.format(cal.getTime()))+" "+tf.format(cal.getTime()));
你要显示日期&amp;时间分别在不同的列中使用:
record.append((d_f.format(cal.getTime()))+", "+tf.format(cal.getTime()));