根据csv中的列属性值获取结果

时间:2016-02-22 17:47:27

标签: java

我试图根据csv文件中的列值(例如ID号)获取行的结果。

让我说我有一个带有这些值的csv文件:

"1.0.0.0,""1.0.0.255"",""16777216"",""16777471"",AU,""Australia"""
"1.0.1.0,""1.0.3.255"",""16777472"",""16778239"",""CN"",""China"""
"1.0.4.0,""1.0.7.255"",""16778240"",""16779263"",AU,""Australia"""
"1.0.8.0,""1.0.15.255"",""16779264"",""16781311"",""CN"",""China"""
"1.0.16.0,""1.0.31.255"",""16781312"",""16785407"",""JP"",""Japan"""

我想选择数组国家[4]为" AU" ,结果应该是这样的:

国家/地区代码= AU,名称=""澳大利亚"" ... 国家/地区代码= AU,名称=""澳大利亚""

这是我的代码:

public void run() {

    String csvFile = "D:/country.csv" ;
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";

    try {

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

                // use comma as separator
            String[] country = line.split(cvsSplitBy);

            System.out.println("...");

            if(country[4]=="AU") { System.out.println("Country code= " + country[4] 
                                 + " , name=" + country[5]);
                                 }

        }

任何想法?谢谢!!

1 个答案:

答案 0 :(得分:0)

为什么不试试以下内容。

if ( country[4].equals( "AU" ) )
{
    System.out.println( "Country code= " + country[4] + " , name=" + country[5] );
}

使用'=='检查对象的引用是否相等。然而,'.equals'函数将比较String的实际内容。