我想从应用中读取csv文件。但我得到java.io.FileNotFoundException:
。以下是我的csv阅读代码。读取CSV文件return 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和文件路径都没有调整任何东西。 –