如果 csv 文件有空值并且我们需要与上列相对应的数据,如何使用逗号分隔它?
我的代码是
BufferedReader fileReader = new BufferedReader(new FileReader(strFileName));
while ((strLine = fileReader.readLine()) != null) {
// [2011.06.28] based on OAP's mail, now ignore "quotations"
strLine = strLine.replace("\"", "");
int j = 0;
String componentID = null;
String analyteResult = null;
lineNumber++;
//break comma separated line using ","
st = new StringTokenizer(strLine, ",");
while (st.hasMoreTokens()) {
//st.nextToken();
String value=st.nextToken();
if(tokenNumber==1)
{
if(lineNumber==1){
batchNo=value;
}
else if(lineNumber==2){
instrumentNo=value;
}
}
else if(lineNumber==4)
{
if(value.contains("_")){
String temp[] = value.split("_");
analyteCode = temp[0].trim();
a[j] = tokenNumber;
j++;
}
}
else if(lineNumber>4)
{
if(tokenNumber==0){
componentID=value;
}
}
tokenNumber++;
//System.out.println("Line Number : "+lineNumber+" Token Number: "+tokenNumber+"Value: "+st.nextToken());
}
//reset token number
tokenNumber = 0;
}
但是我需要与前一列分析物代码相对应的分析结果...因为它也可能为空...因此它会消失所有逗号并且不解析与前一列分析物代码相对应的结果。
My code is
BufferedReader fileReader = new BufferedReader(new FileReader(strFileName));
while ((strLine = fileReader.readLine()) != null) {
// [2011.06.28] based on OAP's mail, now ignore "quotations"
strLine = strLine.replace("\"", "");
int j = 0;
String componentID = null;
String analyteResult = null;
lineNumber++;
//break comma separated line using ","
st = new StringTokenizer(strLine, ",");
while (st.hasMoreTokens()) {
//st.nextToken();
String value=st.nextToken();
if(tokenNumber==1)
{
if(lineNumber==1){
batchNo=value;
}
else if(lineNumber==2){
instrumentNo=value;
}
}
else if(lineNumber==4)
{
if(value.contains("_")){
String temp[] = value.split("_");
analyteCode = temp[0].trim();
a[j] = tokenNumber;
j++;
}
}
else if(lineNumber>4)
{
if(tokenNumber==0){
componentID=value;
}
}
tokenNumber++;
//System.out.println("Line Number : "+lineNumber+" Token Number: "+tokenNumber+"Value: "+st.nextToken());
}
//reset token number
tokenNumber = 0;
}
But I need analyte result corresponding to previous columns analyte codes.... as it could be null also....so its vanishing all commas and not parsing the result corresponding to previous column analyte code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
穆迪特,
我不确定您是否有不使用任何开源库的强迫症,因此我强烈推荐以下广泛使用的开源 CSV 解析器。它将在编码和性能方面为您节省大量精力。请根据您的具体需求查看它们。
Mudit,
I am not sure if you have any compulsion of not using any open sources libs, so I strongly recommend the following widely used open source CSV parsers. It will save you a lot of effort in terms of coding and performance. Pls check them out for your specific need.