-2

My program is to take in a URL that has a CSV file. I'm trying to pass the this into a file for further querying, but it returns with a NoSuchElementException.

public static void main(String[] args) throws ParseException, IOException, URISyntaxException {
    // TODO Auto-generated method stub

    String web = "https://example.com/data.csv";
    URL content = new URL(web);

    File file = new File(content.getPath());

    try {
        Scanner inputStream = new Scanner(file);    //Input file
        String[] values = data.split(",");
        while (inputStream.hasNext()){

            data = inputStream.next();

            values = data.split(",");
            System.out.println(values[];
        }
        inputStream.close();
   } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
1
  • The file is using wrong path. Commented Oct 12, 2017 at 22:32

1 Answer 1

1

from https://docs.oracle.com/javase/7/docs/api/java/net/URL.html

getPath() Gets the path part of this URL. This is not what you want.

openStream() Opens a connection to this URL and returns an InputStream for reading from that connection. This is what you want.

    URL content = new URL(web);
    InputStream stream = content.openStream();

    Scanner inputStream = new Scanner(stream);
    while (inputStream.hasNext()) {
        String data = inputStream.next();
        String[] values = data.split(",");
        System.out.println(values);
    }
    inputStream.close();

Also your url links to gzipped file, which cannot be read directly you need to either download it first, ungzip and treat like normal local file, or provide a link to plain CSV file.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.