1

I am trying to read the csv file from the app. But I get java.io.FileNotFoundException:. Below is my csv reading code.

    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();
    }

below is the error I get.

 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)

Why do I get this error? CSV file is available in the path.

UPDATE

Below is one of an existing code I use in another project. This is working fine. But same code doesn't work in this new project. Got F.N.F error. How this differentiate file input stream is a URL or from a file path?

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());
    }
5
  • is your file available at the path you see printed in your error message or in your classpath? Commented Aug 25, 2016 at 6:27
  • you need to identify the source of your stream. the updated code wont work if the source string is a file location and not http url. Commented Aug 25, 2016 at 9:25
  • @daotan But that is my existing code which is working perfectly fine for both URL and File path without tweak anything. Commented Aug 25, 2016 at 9:41
  • @everalian i dont know how you initialize the 'AppUrlConst.CONFIG_URL' variable but try to use 'file:///C:/somedir/' protocol if csv file is in your drive. Commented Aug 25, 2016 at 9:51
  • @daotan in my development it will take inside the ear. so the URL would be http://localhost:7001/shops/config/whitelist/milRMWhitelist.csv. But in production, it will take from the server location. This behaviour is controlled in the property file. All these are working fine in other project. This is my issue. Commented Aug 26, 2016 at 3:25

4 Answers 4

3

You are mixing FileInputStreams with an HTTP URL

Either use one or the other

e.g.

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

or

 br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
Sign up to request clarification or add additional context in comments.

5 Comments

This path, when I develop locally it is a url path. But when production it's reading from the disk. But my code should remain same to handle both url and disk CSV file.
In that case maybe your should examine the url string to decided which one to use
@everalian You can check if the path is a file using something like this if(new File(filePath).exists()){...}
@ScaryWombat I updated the code in my question area. Why that code work for URL and Filepath? and How does it handle different input streams? That code doesn't work in my new project. I do not know why. That's why I wanted to rewrite this new code.
@Titus Pls refer to my question updated area. That code is in one of my another code. That one works fine for URL or file path. I never have a logic to determine whether it's a URL or file path
0

You are passing a string that represents a URL to your FileInputStream. The FileInputStream expects a file name that points into the file system. Therefore your approach doesn't work!

In other words: by including a host IP address and port number, your string is no longer "just a file name".

Thus there are two choices:

  1. If you really want to pass a URL, then you need to use a real URL object, and use its openStream() method. Meaning: you do not create a FileInputStream from a string; you create a URL object; and then you call openStream() on that object to read from that stream.
  2. You rework your string be just that: a "flat" file name; no ip addresses or port numbers whatsoever.

1 Comment

How to do it? give an example? I tried this final String file = this.httpRequestPoster.sendGetRequest(constantURL, ""); still the same error. When I print the output of the file I see the content of the file. but still has same F.N.F error
0

Based on your comments and the updates to your question I think this will work:

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();
}

The important part is this file.exists() ? new FileInputStream(file) : new URL(constantURL).openStream() which checks if the constantURL is a valid file path on the local filesystem. If it is, it opens a FileInputStream if it is not, it tries to open an url connection to.

Comments

0

I don't know how you declared the AppConst.INTERNAL_ASSETS_CONFIG_ROOT_URL variable

try to use the file:/// protocol in setting the value for file example

URL urlFile = new URL("file:///C:/whitelist/RMWhitelist.csv");
URL urlHttp = new URL("http://http://localhost:7001/shops/config/");

that should work correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.