2013-07-25 39 views
0

我一直在試圖獲得操縱CSV文件的一些幫助。我查看了這個論壇和其他人分析CSV信息到一個新的CSV文件,但我不認爲CSV工程將行和列(即時通訊可能錯誤)。在java中更改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 ...等...這表示流量或第2或第1流量下的第15個值。

我已經注意到,我可能需要添加到API類似opencsv

所以我基本上要串行在0,0上,型號爲0,1角,然後0,2等 偏移值應該與這些數據一致。

非常感謝,迄今爲止我只能得到csv合併,唯一的問題是,他們都像我的第一個例子一樣愚蠢,所以在我合併之前我想要像這樣清理它。

謝謝

+0

如果這是一次性需求,我會在Excel中打開文件並在其中進行操作。 –

+0

對不起,最後一個例子,流量ave [Val 1]應該與它旁邊的0相對應,它不匹配的原因是因爲我從它複製的文件是xls而不是csv。最終文件應該是csv,因此它之間應該有逗號。\ – user2620493

回答

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 
}