我有一个像以下
的CSVElement,Value
LIN,887873083091
QTY,12
LIN,887873083107
QTY,13
LIN,887873083114
QTY,10
LIN,887873083121
QTY,6
LIN,887873083138
QTY,0
Avialability,018
Avail_Date,20391231
LIN,887873083145
QTY,0
Avialability,018
Avail_Date,20391231
LIN,886732553461
QTY,0
Avialability,018
Avail_Date,20391231
LIN,886732637901
QTY,0
Avialability,018
Avail_Date,20391231
LIN,886732637918
QTY,0
Avialability,018
Avail_Date,20391231
LIN,886732637925
QTY,0
Avialability,018
Avail_Date,20391231
LIN,886732637932
QTY,0
Avialability,018
Avail_Date,20391231
LIN,886732637949
QTY,0
Avialability,018
Avail_Date,20391231
LIN,886732637956
QTY,0
Avialability,018
Avail_Date,20391231
LIN,886732637963
QTY,0
Avialability,018
Avail_Date,20391231
LIN,886732637970
QTY,0
Avialability,018
Avail_Date,20391231
正如您所看到的,有4个元素。的 LIN,QTY,可用性Avail_Date
我想将这些元素作为列并在其下面放置值并将其作为CSV文件。
如以下示例
LIN,QTY,Availability,Avail_Date
887873083091,12,,
887873083107,13,,
887873083114,10,,
887873083121,6,,
887873083138,0,018,20391231
887873083145,0,018,20391231
886732553461,0,018,20391231
原始版本遵循LIN,QTY,Availability和Avail_Date的顺序..但在某些情况下可能缺少可用性和Avail_Date。那种情况我想把空白放到新的CSV中,就像样本输出中的前4行一样。
如何为此编写Java代码?谁能给任何解决方案?我没有尝试任何事情,因为我不知道
答案 0 :(得分:0)
这是一个粗略的大纲,可以帮助您入门:
试试吧,如果你遇到困难,请发布你的代码。
答案 1 :(得分:0)
String[] colName = {"LIN","QTY","Availability","Avail_Date"};
String output = "";
int colNum = 0;
// Using file handling read row one by one (readUTF method) : this is sudo code only
// or you can use the CSV parser
while(!EOF)
{
// spilt key and value using ","
for(int i = colNum;i<colName.length;i++){
if(colName[i].equals(key)){
output = output + value + ","
break;
}else{
output = output ","
}
}
colNum = i;
if(colNum == colName.length ){
output = output "\n";
colNum = 0;
}
} // end while
我没有运行此代码。所以可能需要一些小改动。