2016-01-04 152 views
0

我想計算一些列數據並將它作爲列寫入csv文件。然後,在計算其他數據列後,我想將它追加到同一個文件,但是作爲新列。Java將新列添加到csv文件

這裏是我做過什麼:

try { 
    FileWriter writer = new FileWriter(OUT_FILE_PATH, true); 
    for (int i=0; i<data.size(); i++) { 
     writer.append(String.valueOf(data.get(i))); 
     writer.append(","); 
     writer.append("\n"); 
    } 
    writer.flush(); 
    writer.close(); 
} catch (Exception e) {} 

結果 - 這追加的第一列下面的新列,所以我有一個長列。

感謝,

+0

你用什麼的'\ N'?這可能會導致這些問題。 – LordAnomander

+0

@tbrown如果我刪除\ n我得到單行 – michael

+0

我不知道你的數據結構,但是如果你知道列的數量,你可以使用兩個循環,即爲每行的for循環和一行完成後使用'\ n'。 – LordAnomander

回答

1

你必須閱讀您的文件(逐行),然後插入新列的每一行。這裏有一個解決方案使用BufferedReader和BufferedWriter

public void addColumn(String path,String fileName) throws IOException{ 
    BufferedReader br=null; 
    BufferedWriter bw=null; 
    final String lineSep=System.getProperty("line.separator"); 

    try { 
     File file = new File(path, fileName); 
     File file2 = new File(path, fileName+".1");//so the 
        //names don't conflict or just use different folders 

     br = new BufferedReader(new InputStreamReader(new FileInputStream(file))) ; 
     bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2))); 
     String line = null; 
        int i=0; 
     for (line = br.readLine(); line != null; line = br.readLine(),i++) 
     {    

      String addedColumn = String.valueOf(data.get(i)); 
      bw.write(line+addedColumn+lineSep); 
    } 

    }catch(Exception e){ 
     System.out.println(e); 
    }finally { 
     if(br!=null) 
      br.close(); 
     if(bw!=null) 
      bw.close(); 
    } 

} 
+0

基本上你建議每次寫入一個新的文件-tmp文件,並在最後附加到包含所有其他好的列的最終文件? – michael

+0

或者只是刪除舊文件並將新文件重命名爲原始名稱。 –

+0

你的新文件已經擁有了一切。 –

0

希望這會幫助你。

{ 
    //CREATE CSV FILE 
    StringBuffer csvReport = new StringBuffer(); 
    csvReport.append("header1,Header2,Header3\n"); 
    csvReport.append(value1 + "," + value2 + "," + value3 + "\n"); 

    generateCSVFile(filepath,fileName, csvReport); // Call the implemented mathod 
} 

public void generateCSVFile(String filepath,String fileName,StringBuffer result) 
{ 
    try{ 

    FileOutputStream fop = new FileOutputStream(filepath); 

    // get the content in bytes 
    byte[] contentInBytes = result.toString().getBytes(); 

    fop.write(contentInBytes); 
    fop.flush(); 

    //wb.write(fileOut); 
    if(fop != null) 
     fop.close(); 
    }catch (Exception ex) 
    { 
     ex.printStackTrace(); 
    } 
} 
+0

不好回答。但如果他想要寫入不是一行一行,而是逐列逐行輸出,他做了什麼? –

1

像這樣的東西可能:

public void appendCol(String fileName, ???ArrayList??? data) { //assuming data is of type ArrayList here, you need to be more explicit when posting code 

    String lineSep = System.getProperty("line.separator"); 
    String output = ""; 
    try{ 
     BufferedReader br = new BufferedReader(new FileReader(fileName)); 
     String line = null; 
     int i = 0; 
     while ((line = br.readLine()) != null) { 
      output += line.replace(
        lineSep, 
        "," + String.valueOf(data.get(i)) + lineSep); 
      i++; 
     } 
     br.close(); 
     FileWriter fw = new FileWriter(fileName, false); //false to replace file contents, your code has true for append to file contents 
     fw.write(output); 
     fw.flush(); 
     fw.close(); 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } 
}