私の場合、有効な CSV はコンマまたはセミコロンで区切られたものです。私は他のライブラリを受け入れていますが、それは Java である必要があります。Apache CSVParser API を読んで、私が考えることができる唯一のことは、非効率的で醜いように見えるこれを行うことです。
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(file));
CSVFormat csvFormat = CSVFormat.EXCEL.withHeader().withDelimiter(';');
CSVParser parser = csvFormat.parse( reader );
// now read the records
}
catch (IOException eee)
{
try
{
// try the other valid delimeter
csvFormat = CSVFormat.EXCEL.withHeader().withDelimiter(',');
parser = csvFormat.parse( reader );
// now read the records
}
catch (IOException eee)
{
// then its really not a valid CSV file
}
}
最初に区切り文字を確認する方法、またはおそらく 2 つの区切り文字を許可する方法はありますか? 例外をキャッチするよりも良いアイデアはありますか?