2016-08-25 133 views
1

我想從應用中讀取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()); 
    } 
+0

是你的文件在你看到的打印在你的錯誤信息或類路徑中的路徑上嗎? – LuckAss

+0

您需要確定您的信息流的來源。如果源字符串是文件位置而不是http url,則更新的代碼將不起作用。 – daotan

+0

@daotan但是,這是我現有的代碼工作完美的URL和文件路徑都沒有調整任何東西。 –

回答

0

你傳入一個表示URL您的FileInputStream的字符串。 FileInputStream需要一個指向文件系統的文件名。因此你的方法不起作用!

換句話說:通過包含主機IP地址和端口號,您的字符串不再是「僅僅是文件名」。

因此,有兩種選擇:

  1. 如果你真的想通過一個URL,那麼你需要使用一個真正的URL對象,並使用它的OpenStream()方法。含義:你做不是從一個字符串創建一個FileInputStream;你創建一個URL對象;然後您從該流上調用該對象的openStream()到,
  2. 你重做你的字符串就是這樣:一個「扁平」的文件名;沒有任何IP地址或端口號。
+0

如何做到這一點?舉個例子?我試過這個'final String file = this.httpRequestPoster.sendGetRequest(constantURL,「」);'仍然是同樣的錯誤。當我打印'file'的輸出時,我會看到文件的內容。但仍然有相同的F.N.F錯誤 –

+0

看到我更新的答案。 – GhostCat

2

你混合FileInputStreams與HTTP URL

既可以使用一種或另一種

例如

InputStream input = new URL(constantUrl).openStream(); 

br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath))); 
+0

這個路徑,當我在本地開發它是一個url路徑。但是當它正在從磁盤讀取時。但我的代碼應該保持相同,以處理url和磁盤CSV文件。 –

+0

在這種情況下,也許你應該檢查url字符串,以決定使用哪一個 –

+1

@everalian你可以檢查路徑是否使用類似這樣的文件'if(new File(filePath).exists()){... }' – Titus

0

根據您的意見,我想更新你的問題,這將工作:

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連接。

0

我不知道你是如何聲明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/"); 

應該正常工作設定值。