【发布时间】:2017-01-01 11:04:57
【问题描述】:
我正在尝试从应用程序中读取 csv 文件。但我得到java.io.FileNotFoundException:。下面是我的csv阅读代码。
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 文件在路径中可用。
更新
以下是我在另一个项目中使用的现有代码之一。这工作正常。但是相同的代码在这个新项目中不起作用。得到 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());
}
【问题讨论】:
-
您的文件是否在错误消息或类路径中显示的路径中可用?
-
您需要确定流的来源。如果源字符串是文件位置而不是 http url,则更新后的代码将不起作用。
-
@daotan 但这是我现有的代码,对于 URL 和文件路径都可以正常工作,无需进行任何调整。
-
@everalian 我不知道您如何初始化“AppUrlConst.CONFIG_URL”变量,但如果您的驱动器中有 csv 文件,请尝试使用“file:///C:/somedir/”协议。跨度>
-
@daotan 在我的开发中它会进入耳朵。所以 URL 是
http://localhost:7001/shops/config/whitelist/milRMWhitelist.csv。但在生产中,它将从服务器位置获取。此行为在属性文件中进行控制。所有这些在其他项目中都可以正常工作。这是我的问题。