【问题标题】:string splitted by comma in java csv filejava csv文件中用逗号分隔的字符串
【发布时间】:2018-04-06 06:48:31
【问题描述】:

这似乎是一个很容易解决的问题,但我一直坚持下去。任何人都可以帮助我并解释我在哪里做错了吗? 我读了一个 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();
        }

    }
}

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

【问题讨论】:

    标签: java split csv


    【解决方案1】:

    您应该使用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();
            }
        }
    

    【讨论】:

    • 非常感谢@AlpeshJikadra 它适用于 nextLine()
    【解决方案2】:

    在这里,我使用 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();
            }
    
        }
    }
    

    【讨论】:

    • 不要添加代码,只描述你改变了什么以及为什么
    【解决方案3】:

    作为附加提示,我认为,使用 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。

    【讨论】:

    • 谢谢@AlexeyGavluk。我是 Java 新手,所以每个提示对我都有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    相关资源
    最近更新 更多