首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正在读取CSV文件返回java.io.FileNotFoundException:

正在读取CSV文件返回java.io.FileNotFoundException:
EN

Stack Overflow用户
提问于 2016-08-25 14:25:51
回答 4查看 1.1K关注 0票数 1

我正在尝试从应用程序中读取csv文件。但我得到了java.io.FileNotFoundException:。下面是我的csv阅读代码。

代码语言:javascript
运行
复制
    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();
    }

下面是我得到的错误。

代码语言:javascript
运行
复制
 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还是来自文件路径?

代码语言:javascript
运行
复制
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());
    }
EN

Stack Overflow用户

发布于 2016-08-25 14:31:23

您正在将FileInputStreams与HTTP URL混合在一起

要么使用一个,要么使用另一个

例如:

代码语言:javascript
运行
复制
 InputStream input = new URL(constantUrl).openStream();

代码语言:javascript
运行
复制
 br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
票数 2
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39138096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档