读取 CSV 文件并将值拆分为 java

Read a CSV file and split values in java

你好,我想读一个 file.csv 包含这样的单词对

1 AA 9877.88,BY US,SA 6785.66,....

我想按以下格式拆分该数据

9877.88,US,6785.66,.........

我已经写了下面几行代码来读取csv文件

do{
    currentline = br.readline()
    
    if(currentline != null)
    {
    
    arrayStrFileRows = currentline.split(",");
}

谁能帮我按要求整理数据。提前致谢

用逗号分隔行后,用 SPACE 分隔该行中的每个字段并取最后一个元素。

currentline = br.readline()
… for each line of input 
List< String > newFields= new ArrayList<>() ;
String[] fieldsOfRow = currentline.split(",");
for( String field : fieldsOfRow )
{
    String[] partsOfField = field.split( " " ) ;
    String lastPart = partsOfField[ partsOfField.length - 1 ] ;
    newFields.add( lastPart ) ;
    …
}
String newLine = String.join( "," , newFields ) ;

我没有足够的声誉来发表评论,但看起来您可能想要进一步拆分字符串数组 arrayStrFileRows 中的字符串 space 并附加您需要的值您将打印的新字符串。

arrayStrFileRows = currentline.split(",");
String result ="";

for (int i=0; i<arrayStrFileRows.length(); i++){
    String[] tokens = arrayStrFileRows.split(" ");
    if (i ==0){
         result = tokens[2]    //i m not too sure about this since there are 3 words/numbers before your first comma and is the 1 supposed to be there or not?
    }else{
         result = result +","+token[1];
    }
}

}