How to programmatically guess whether a CSV file is comma or semicolon delimited

前端 未结 5 1777
滥情空心
滥情空心 2021-01-04 08:29

In most cases, CSV files are text files with records delimited by commas. However, sometimes these files will come semicolon delimited. (Excel will use semicolon delimiter

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-04 09:13

    You can read the first line

    FileReader fileReader = new FileReader(filePath);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String s = bufferedReader.readLine();
        String substring = s.substring(s.indexOf(firstColumnName) + 3, s.indexOf(firstColumnName) + 4);
        bufferedReader.close();
        fileReader.close();
        substring.charAt(0);
    

    Then you capture this value

    substring.charAt(0)

    depending on whether the CSV is comma or semicolon can use the last value

提交回复
热议问题