我正在使用csvwriter以java格式获取csv格式的输出。
我有一个以下列格式返回数据的程序:
result didNumber businessName actualNumber
91930921800 TEMP NAME
917930921800 TEMP NAME
NOANSWER 917930921800 TEMP NAME 9845618737
NOANSWER 917930921800 TEMP NAME 9845618737
NOANSWER 917930921800 TEMP NAME 8951577970
NOANSWER 917930921801 TEMP NAME 9920036823
NOANSWER 917930921802 TEMP NAME 8762224646
NOANSWER 917930921802 TEMP NAME 9845618737
NOANSWER 917930921803 TEMP NAME 9448017755
NOANSWER 917930921803 TEMP NAME 9845618737
NOANSWER 917930921895 TEMP NAME 9448017755
DIALING 917930921800 TEMP NAME 8951577970
DIALING 917930921802 TEMP NAME 8762224646
这是我在csv文件中编写的java代码。
public void writeCSVData(ResultSet cdrs,String fileName) throws IOException, SQLException {
String methodName = "writeCSVData";
logger.debug("entering in " + methodName + " in " + className);
BufferedWriter writer = new BufferedWriter(new FileWriter(createFile(fileName).getAbsoluteFile()));
CSVWriter csvWriter = new CSVWriter(writer);
String currentDid = null;
String prevDid = null;
String businessName = null;
String businessReference = null;
String actualNumber = null;
StringBuilder result = new StringBuilder();
String[] entries = null;
int count = 0;
while (cdrs.next()) {
currentDid = cdrs.getString("didNumber");
if (count == 0){
prevDid = currentDid;
result.append(currentDid);
result.append(",");
businessName = cdrs.getString("businessName");
result.append(businessName);
result.append(",");
businessReference = cdrs.getString("businessReference");
result.append(businessReference);
result.append(",");
actualNumber = cdrs.getString("actualNumber");
result.append(actualNumber);
count++;
continue;
}
if(!prevDid.equals(currentDid)){
entries = result.toString().split(",");
result.append("\n");
csvWriter.writeNext(entries);
prevDid = currentDid;
currentDid = cdrs.getString("didNumber");
result.append(currentDid);
result.append(",");
businessName = cdrs.getString("businessName");
result.append(businessName);
result.append(",");
businessReference = cdrs.getString("businessReference");
result.append(businessReference);
result.append(",");
actualNumber = cdrs.getString("actualNumber");
result.append(actualNumber);
result.append(",");
continue;
}
if(prevDid.equals(currentDid)){
prevDid = currentDid;
result.append(",");
actualNumber = cdrs.getString("actualNumber");
result.append(actualNumber);
result.append(",");
continue;
}
}
}
基本上我在一行中写了didNumber
的所有实际数字,我正在把它写到csv。
响应是正确的,只有每一行重复不止一次。
这是我的输出:
"917930921800","TEMP NAME","null","null","null","","9845618737","","8951577970","","9845618737","","8951577970"
"917930921800","TEMP NAME","null","null","null","","9845618737","","8951577970","","9845618737","","8951577970","
917930921801","TEMP NAME","null","9920036823"
"917930921800","TEMP NAME","null","null","null","","9845618737","","8951577970","","9845618737","","8951577970","
917930921801","TEMP NAME","null","9920036823","
917930921802","TEMP NAME","null","8762224646","","8762224646","","9845618737"
"917930921800","TEMP NAME","null","null","null","","9845618737","","8951577970","","9845618737","","8951577970","
917930921801","TEMP NAME","null","9920036823","
917930921802","TEMP NAME","null","8762224646","","8762224646","","9845618737","
917930921803","TEMP NAME","null","9448017755","","9845618737
理想情况下,响应应该是这样的:
"917930921800","TEMP NAME","null","null","null","","9845618737","","8951577970","","9845618737","","8951577970"
917930921801","TEMP NAME","null","9920036823","
917930921802","TEMP NAME","null","8762224646","","8762224646","","9845618737"
917930921803","TEMP NAME","null","9448017755","","9845618737
请告诉我可能做错了什么。
答案 0 :(得分:0)
我建议你像Lutz建议的那样使用像opecsv这样的库。
可能存在的问题在这里。
StringBuilder result = new StringBuilder();
Yoou在循环之外删除了这一行,并且在写完字符串后没有重置它。
在csvWriter.writeNext(entries);
result.delete(0, result.length());
它会重置字符串。