java csv文件中用逗号分隔的字符串

时间:2018-04-06 06:48:31

标签: java csv split delimiter-separated-values

解决这个问题似乎很容易,但我坚持下去。任何人都可以帮助我并解释我在哪里犯了错误? 我读了一个csv文件:

Time,Activ,Switch,A,B,C,D,E
5:58,on,,,,,,
6:00,on 123,,,ab cd,,,
6:02,on - off,1->2,6,ab cd,1,,
6:04,on,,,cd ab,1,,
6:06,off,,,,,,
7:22,on / off,,,,,,
7:30,on / of,3->4,15,,,,
7:34,home on,,,ab cd,,,

我想用逗号分隔行中的数据并保存到看起来像(第一行省略)的数组中:

1---> ["5:58","on", "", "", "", "", "", ""]
2---> ["6:00", "on 123", "", "", "ab cd", "", "", ""]
3---> ["6:02", "on - off", "1->2", "6", "ab cd", "1", "", ""]
6---> ["7:22", "on / off", "3->4", "15", "", "", "", ""]

依此类推...但是我也收到了带有分割字符串的数组,也包括空格和其他字符

1---> ["5:58","on", "", "", "", "", "", ""]
2---> ["6:00", "on", "123", "", "", "ab", "cd", "", "", ""]
3---> ["6:02", "on", "-", "off", "1->2", "6", "ab", "cd", "1", "", ""]
6---> ["7:22", "on", "/", "off", "3->4", "15", "", "", "", ""]

这是我的代码:

public class Test {
    public static void main(String []args) {

    String fileName = "TestData.csv";
    File file = new File(fileName);

    try{
        Scanner inputStream = new Scanner(file);
        String firstLine = inputStream.next();
            while(inputStream.hasNext()){
                String dataRow = inputStream.next();

                String[] valuesInDataRow = dataRow.split(",",-1);

                for(int i=0;i<valuesInDataRow.length;i++){
                    System.out.println("---> " + valuesInDataRow[i]);
                }

            }
            inputStream.close();
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

我会非常感谢任何解释:)

3 个答案:

答案 0 :(得分:2)

您应该使用inputStream.nextLine()代替inputStream.next()

尝试类似

的内容
 public static void main(String[] args) {
        String fileName = "TestData.csv";
        File file = new File(fileName);

        try{
            Scanner inputStream = new Scanner(file);
            String firstLine = inputStream.nextLine();
            while(inputStream.hasNext()){
                String dataRow = inputStream.nextLine();

                String[] valuesInDataRow = dataRow.split(",",-1);

                for(int i=0;i<valuesInDataRow.length;i++){
                    System.out.println("---> " + valuesInDataRow[i]);
                }

            }
            inputStream.close();
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }
    }

答案 1 :(得分:1)

在这里,我使用了Scanner.nextLine()和regx \\s*,\\s*来删除不需要的空白区域。

public class Test {
    public static void main(String []args) {

        String fileName = "TestData.csv";
        File file = new File(fileName);

        try{
            Scanner inputStream = new Scanner(file);
            String firstLine = inputStream.nextLine();
            while(inputStream.hasNext()){
                String dataRow = inputStream.nextLine();

                String[] valuesInDataRow = dataRow.split("\\s*,\\s*", -1);

                for(int i=0;i<valuesInDataRow.length;i++){
                    System.out.println("---> " + valuesInDataRow[i]);
                }

            }
            inputStream.close();
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

答案 2 :(得分:0)

正如另外的提示,我认为,使用Java 1.7功能“try-for-resource”将是一个好建议

https://www.journaldev.com/592/java-try-with-resources

而不是:

try{
   Scanner inputStream = new Scanner(file);
   ...
   inputStream.close();
}
catch(FileNotFoundException e) {
  ...
}

总是更好用:

try(Scanner inputStream = new Scanner(file)) {
...
}
catch (IOException e) {
...
}

特别是如果文件被认为是大的话。但是可以使用Java 1.7。