读取CSV文件return java.io.FileNotFoundException:
我想从应用中读取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());
}
你传入一个表示URL您的FileInputStream的字符串。 FileInputStream需要一个指向文件系统的文件名。因此你的方法不起作用!
换句话说:通过包含主机IP地址和端口号,您的字符串不再是“仅仅是文件名”。
因此,有两种选择:
- 如果你真的想通过一个URL,那么你需要使用一个真正的URL对象,并使用它的OpenStream()方法。含义:你做不是从一个字符串创建一个FileInputStream;你创建一个URL对象;然后您从该流上调用该对象的openStream()到,。
- 你重做你的字符串就是这样:一个“扁平”的文件名;没有任何IP地址或端口号。
如何做到这一点?举个例子?我试过这个'final String file = this.httpRequestPoster.sendGetRequest(constantURL,“”);'仍然是同样的错误。当我打印'file'的输出时,我会看到文件的内容。但仍然有相同的F.N.F错误 –
看到我更新的答案。 – GhostCat
你混合FileInputStreams与HTTP URL
既可以使用一种或另一种
例如
InputStream input = new URL(constantUrl).openStream();
或
br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
这个路径,当我在本地开发它是一个url路径。但是当它正在从磁盘读取时。但我的代码应该保持相同,以处理url和磁盘CSV文件。 –
在这种情况下,也许你应该检查url字符串,以决定使用哪一个 –
@everalian你可以检查路径是否使用类似这样的文件'if(new File(filePath).exists()){... }' – Titus
根据您的意见,我想更新你的问题,这将工作:
String constantURL = AppConst.INTERNAL_ASSETS_CONFIG_ROOT_URL + "whitelist/RMWhitelist.csv";
File file = new File(constantURL);
String cvsSplitBy = ",";
try(BufferedReader br = new BufferedReader(new InputStreamReader(file.exists()?new FileInputStream(file): new URL(constantURL).openStream()));){
for (String line; (line = br.readLine()) != null;) {
String[] country = line.split(cvsSplitBy);
System.out.println("Country [code= " + country[0] + " , name=" + country[1] + "]");
}
}catch(Exception e){
e.printStackTrace();
}
最重要的部分是这个file.exists() ? new FileInputStream(file) : new URL(constantURL).openStream()
它检查是否constantURL
是本地文件系统上的有效文件路径。如果是,它会打开一个FileInputStream
,如果不是,它会尝试打开一个url连接。
我不知道你是如何声明AppConst的。INTERNAL_ASSETS_CONFIG_ROOT_URL可变
尝试使用file:///协议文件 例如
URL urlFile = new URL("file:///C:/whitelist/RMWhitelist.csv");
URL urlHttp = new URL("http://http://localhost:7001/shops/config/");
应该正常工作设定值。
是你的文件在你看到的打印在你的错误信息或类路径中的路径上吗? – LuckAss
您需要确定您的信息流的来源。如果源字符串是文件位置而不是http url,则更新的代码将不起作用。 – daotan
@daotan但是,这是我现有的代码工作完美的URL和文件路径都没有调整任何东西。 –