アプリからcsvファイルを読み取ろうとしています。しかし、私はjava.io.FileNotFoundException:
を得ます。以下は私のcsvの読書コードです。CSVファイルを読み取ると、java.io.FileNotFoundExceptionが返されます。
String constantURL = AppConst.INTERNAL_ASSETS_CONFIG_ROOT_URL + "whitelist/RMWhitelist.csv";
logger.info("constantURL > " + constantURL);
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try{
br = new BufferedReader(new InputStreamReader(new FileInputStream(constantURL)));
while ((line = br.readLine()) != null) {
String[] country = line.split(cvsSplitBy);
System.out.println("Country [code= " + country[0] + " , name=" + country[1] + "]");
}
}catch(Exception e){
e.printStackTrace();
}
下記のエラーが発生します。
INFO: constantURL > http://localhost:7001/shops/config/whitelist/milRMWhitelist.csv
java.io.FileNotFoundException: http:\localhost:7001\app\config\whitelist\RMWhitelist.csv (The filename, directory name, or volume label syntax is incorrect)
なぜこのエラーが発生しますか?パスにはCSVファイルがあります。
UPDATE
以下は、私は別のプロジェクトで使用する既存のコードの一つです。これはうまくいきます。しかし、この新しいプロジェクトでは同じコードは機能しません。 F.N.Fエラーが発生しました。このファイル入力ストリームをURLと区別する方法、またはファイルパスから区別する方法
final String file = this.httpRequestPoster.sendGetRequest(AppUrlConst.CONFIG_URL + "other.csv", "");
Reader reader = new StringReader(file);
final CSVReaderBuilder<UpgradeVO> customerVOCSVReaderBuilder = new CSVReaderBuilder<UpgradeVO>(reader);
customerVOCSVReaderBuilder.strategy(CSVStrategy.UK_DEFAULT);
CSVReader<UpgradeVO> customerVOCSVReader = customerVOCSVReaderBuilder.entryParser(new UpgradeParser()).build();
Iterator<UpgradeVO> customerVOIterator = customerVOCSVReader.iterator();
while (customerVOIterator.hasNext()) {
UpgradeVO upgradeVO = customerVOIterator.next();
logger.info(UpgradeVO.getServiceId());
}
あなたのエラーメッセージまたはクラスパスに表示されているパスに、ご使用のファイルがありますか? – LuckAss
ストリームのソースを特定する必要があります。ソース文字列がhttp URLでなくファイルの場所である場合、更新されたコードは機能しません。 – daotan
@daotanしかし、それはURLとファイルパスの両方を調整することなく完全に正常に動作する私の既存のコードです。 –