2016-04-06 15 views
2

javaを使用して最初の行の最後にタブで区切られた「閉じる」という単語を削除する必要があります。残りのファイルは変更されません。 どうすればいいですか?Javaでタブ付きの列csvファイルを削除する方法

"Close" is tabbed

私はこのコードを適応しようとしている:

public class removezero{ 
    public static void main(String[] args) throws Exception { 

    //scanner to read csv file 
    Scanner replace = new Scanner(new File("csvfile")); 


    //array of string 
    String [][] all= new String [10][10]; 

    //new csv file to write answer 
    FileWriter removezero = new FileWriter(createReplacedCsv()); 

    int i=0; 
    StringBuilder builder = new StringBuilder(); 

    //write csv to array 




    while (replace.hasNext()) { 

       String[] result = replace.nextLine().split(","); 

       all[i]= result; 
       i++; 


    } 


     // write from array to builder 

    for(int j=0;j<5; j++){ 
     for(int k=0;k<5; k++) 
      if(!(all[1][k]).equals("0")){builder.append(all[j][k]).append(",");} 
      builder.deleteCharAt(builder.lastIndexOf(",")); 
      builder.append("\n"); 
    } 


    //write to csv file 
     removezero.write(builder.toString()); 
     removezero.flush(); 
     removezero.close(); 

    } 




private static File createReplacedCsv() throws Exception { 
    File replacedCsv = new File("removedzero.csv"); 
    replacedCsv.createNewFile(); 
    return replacedCsv; 
} 
} 

おかげ

答えて

0

は、定義したと言う:

private static String removeTabbedColumn(String line) { 
    int indexOfTab = line.indexOf('\t'); 
    if (indexOfTab == -1) { 
     return line; 
    } else { 
     return line.substring(0, indexOfTab); 
    } 
} 

今、あなたは

を使用することができます

私はテストしていません。

0
あなたがCsvParserを使用していないのはなぜ

、例えば https://commons.apache.org/proper/commons-csv/

final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); 
try { 
    for (final CSVRecord record : parser) { 
     final String string = record.get("SomeColumn"); 
     // Get the intended columns here. 
    } 
関連する問題