在String

时间:2017-01-28 22:54:47

标签: java

以下是我的Java代码。我正在传递CSV文件并阅读记录。我想通过","分割记录。并将其存储到一个数组中。我不知道该怎么做。如果有人可以给我这个想法。我对Java很陌生。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class parser 
{
    public static void main(String[] args) throws IOException 
    {
        FileReader fileReader = new FileReader(new File("//Users//Desktop//npi.csv"));
        BufferedReader br = new BufferedReader(fileReader);
        String line = null;
        //String[] value = line.split(",");

        /*ArrayList list = new ArrayList(Arrays.asList(line));
        System.out.println(list);*/
        int count = 0;
        while((line = br.readLine()) != null)
        {
            for(int i=0; i < line.length();i++)
            {

            }
            count ++;
            System.out.println("Record Count : "+count);
            System.out.println(line);
        }

    }
}

CSV文件中的字符串示例

  

&#34; 1234567&#34;&#34; 1&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34; ALI&#34;&#34; DONALDSON&#34;&#34;&#34;&#34; DR&#34;&#34;&#34;&#34; OD #&34;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;, &#34;&#34;,&#34;&#34;,&#34;&#34;,&#34;&#34;,&#34; 140-6002 SIT RD&#34;,& #34; STE   A&#34;&#34; EVANSVILLE&#34;&#34; IN&#34;&#34; 8418996408&#34;&#34; US&#34;&#34; 6013857469&#34 ;, &#34; 9586771232&#34;,&#34; 140-6002 SIT RD&#34;,&#34; SUITE   A&#34;&#34; EVANSVILLE&#34;&#34; IN&#34;&#34; 770155917&#34;&#34; US&#34;&#34; 6969551228&#34 ;, &#34; 2403884068&#34;&#34; 04/13/2003&#34;&#34; 02 /二千〇一十八分之二十一&#34;&#34;&#34;&#34;&# 34;,&#34;&#34;&#34; M&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;&#34;&#34;&#34;&#34; 142K01234Z&#34;&#34; 5376UD&#34;&#34; IN&#34;&#34; Y&#34 ;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34; &#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34; #&34;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;, &#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;& #34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;,& #34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;&#34;&#34; 980230944&#34;&#34; 03&#34;&#34; IN&#34;&#34;&#34;&#34; W R09447&#34;&#34; 01&#34;&#34; IN&#34;&#34;&#34;&#34; 12532T&#34;&#34; 02&#34 ;, &#34; IN&#34;&#34; GROUP&#34;&#34; 245699532&#34;&#34; 08&#34;&#34; IN&#34;&#34;& #34;&#34; 3HDPTO&#34;&#34; 09&#34;&#34; IN&#34;&#34;个人&#34;&#34;&#34;,& #34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34; &#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34; #&34;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;, &#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;& #34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;,& #34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;,&#34;&#34;&#34;&#34;,& #34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34; &#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34; #&34;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;, &#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;& #34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;,& #34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34; &#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34; #&34;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;, &#34;&#34;&#34;&#34 ;, &#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;& #34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;,& #34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;,&#34;&#34;&#34;&#34;&#34; X&#34;&#34;&#34;&#34;&#34;&# 34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;,&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34 ;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34; &#34;&#34;&#34;&#34;

4 个答案:

答案 0 :(得分:0)

使用&#34;,&#34;分割每一行。像这样的分隔符:

String[] records = line.split(",");

答案 1 :(得分:0)

您已使用Arrays.asList(line),但您应Arrays.asList(value)。还在while循环中注释了代码位置。

List是一个接口,ArrayList是一个实现。您不需要对象的实例类型来使用数据。只需使用界面。此外,您应该知道编码接口是Java中的最佳实践,也是OOP的一个原则。

对于需要List<String>的单行,要保留这些行,您应该使用另一个列表List<List<String>>。您可以逐行实例化并向其添加列表。

答案 2 :(得分:0)

在您的代码中是错误的:您正在尝试拆分null的字符串。将其移动到while循环中,其中line指的是实际的String对象。

您希望将字符串拆分为",",但我想您还想删除",如果它是第一个或最后一个字符。

可以使用String.split(String regex)

来完成
String[] split = x.split("(\",\"|\\A\"|\"\\z)");

此正则表达式在任何地方匹配",",在字符串的开头/结尾处匹配"。如果输入以引号开头,则结果数组以空字符串开头。

请注意,使用

输出数组要容易得多
System.out.println( Arrays.toString(myArray) );

答案 3 :(得分:-1)

首先定义一个列表,您可以在其中存储所有单词:

List<String> list = new ArrayList<String>();

您的for循环内不需要while循环。摆脱它并改为使用以下方法:

//Remove quotes. I think you wouldn't need them.
line = line.replace("\"", "");
//Now get the words which are separated by a comma
String[] words = line.split(",");
//Now add all the words to the main list
Collections.addAll(list, words);