我一直试图在操作CSV文件方面获得一些帮助。我已经浏览了这个论坛和其他人将CSV信息解析为一个新的CSV文件,但我不认为CSV工作会行和列(我可能错了)。
无论如何,我有一个看起来像这样的CSV
\CAL 0.75-12188 06-28-2013 12-05.csv
0
6-28-2013 12-05
Serial Nbr,0.75-12188
Sales Order Nbr,23966
Model Nbr,3/4" LDP
Tag Name,LX-2-AHU-7H
Angle Offset,7.11
Overshoot,0
Flow Rate Closing,0.00000000,0.46367650,0.61821940,1.08192100,1.39102900,2.16381500,2.93662600,3.86397900,4.79133200,5.87325100,7.10973700,8.50076700,9.58268700,10.51004000,11.12828000,12.21020000,
Flow Rate Opening,0.00000000,0.46367650,0.61821940,1.08192100,1.23646200,2.00927400,2.62749100,3.55486900,4.48222200,5.40957500,6.49149300,7.72795900,8.80987500,9.58268700,10.66460000,11.90107000,
Flow Rate Ave,0.00000000,0.46367650,0.61821940,1.08192100,1.31374500,2.08654400,2.78205900,3.70942400,4.63677700,5.64141300,6.80061500,8.11436300,9.19628100,10.04636000,10.89644000,12.05564000,
Angle Closing,18.67788000,24.13643000,29.82170000,35.43774000,41.11793000,46.74594000,52.17502000,57.80665000,63.35862000,68.93623000,74.51913000,80.14727000,85.78853000,91.05897000,95.72566000,100.62130000,
Angle Opening,18.67518000,23.91829000,29.50020000,35.29157000,40.52844000,46.27448000,51.63242000,57.84357000,63.00760000,68.66663000,74.01138000,79.50329000,85.10954000,90.60167000,96.38528000,100.58310000,
Angle Ave,18.67653000,24.02736000,29.66095000,35.36465000,40.82319000,46.51021000,51.90372000,57.82511000,63.18311000,68.80144000,74.26526000,79.82528000,85.44904000,90.83032000,96.05547000,100.60220000,
我希望它看起来像这样(我认为它是一个不同的文件,但所有文件的长度和大小都相同。)
Serial # Model # Tag Name Angle Offset Flow Rate Ave [val 1] Flow Rate Ave [val 2] Flow Rate Ave [val 15]
0.75-12188 3/4" LDP LX-2-AHU-7H 7.11 0 0.4636765 10.89644
0.75-12192 0.75 LDP - 11 NC-P-AHU-4H 12.92 0 0.3091087 11.20555
您会注意到流量为val 1,2,15 ...等...这表示流量下的第15个值或第2个或第1个值。
我注意到我可能需要添加像opencsv
这样的api所以我基本上希望序列在0,0中,型号为0,1角度偏移在0,2等 那么这些值应与这些数据相对应。
非常感谢,到目前为止,我只能获得csv合并,唯一的问题是它们像我的第一个例子一样愚蠢,所以在合并之前我想要像这样清理它。
谢谢,
乔
答案 0 :(得分:0)
创建一个包含您列出的所有变量的对象,即Serial Number, Model Number ...etc
。
作为该对象的构造函数,您可以在文件名中进行解析,然后构造函数将读取该文件并根据需要填充对象,例如......
public YourObject(String filename)
{
try
{
//Create a file reader
BufferedReader inputStream = new BufferedReader(new FileReader(filename));
// Read each line and treat appropriately
String line;
while((line = inputStream.readLine()) != null)
{
String[] splArry = line.split(",");
if(splArry.length > 1)
{
// set fields appropriately here
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
然后,您可以创建一个方法来构建csv字符串,类似于
public String toCSVString()
{
String out = "";
out += serailNumber + "\t"; // or what ever delimiter you want
//... etc for all your fields
return out;
}
然后,如果您有一个包含所有这些文件的文件夹,您只需为每个对象构建一个对象,将其添加到列表中,然后为列表中的每个对象调用CSVString方法并将其附加到主csv文件。
File folder = new File("The folder where your files are")
Files[] files = folder.listFiles();
for(File f: files)
{
YourObject yourOb = new YourObject(f.getName()); //just realised propbably would be better to create constructer which takes file not the filename;
String csv = yourObj.toCSVString(); // and then wrtie this to your new csv file
}