0

I have a problem reading a csv file from the web. I get a File not found exception. That's the source: http://data.okfn.org/data/core/s-and-p-500-companies/r/constituents.csv How could it be the file cannot be found if I can easily open it ? what am I missing here ?

package Investing;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {

   public static void main(String[] args) {

    String csvFile = "http://data.okfn.org/data/core/s-and-p-500-     companies/r/constituents.csv";
    String line = "";
    String cvsSplitBy = ",";


    try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

        while ((line = br.readLine()) != null) {

            // use comma as separator
            String[] data = line.split(cvsSplitBy);

            System.out.println(data);

        }

    } catch (IOException e) {
        e.printStackTrace();
    }


}
}
5
  • Download that file onto local manually and then use filereader. Commented Jul 31, 2016 at 9:49
  • @SMA The content of the file can change, that's why I want to read the data directly from the web. There's no option of doing so ? Commented Jul 31, 2016 at 9:53
  • 1
    Then better you read from url or download the file and then read the file. Commented Jul 31, 2016 at 9:57
  • @SMA can you add the relevant code to do one of the options (reading from a url or downloading the file) ? Commented Jul 31, 2016 at 10:06
  • @SMA I managed to read the file from the URL.Thanks! God bless the internet :) Commented Jul 31, 2016 at 10:16

1 Answer 1

1

FileReader is used for local files. See: Read remote .csv file using opencsv for reading a remote cvs file.

Another alternative to read a remote file in:

public static void main(String[] args) {

    String csvFile = "http://data.okfn.org/data/core/s-and-p-500-companies/r/constituents.csv";

    try {
        URL url12 = new URL(csvFile);
        URLConnection urlConn = url12.openConnection();
        InputStreamReader inStream = new InputStreamReader(urlConn.getInputStream());
        BufferedReader buff = new BufferedReader(inStream);

        String line = buff.readLine();
        line  = buff.readLine();
        while (line != null) {

            System.out.println(line);
            line = buff.readLine();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

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

3 Comments

Yeah, I know. I used that code and changed it a bit.
What's the difference between opening a connection and getting an inputStream, and opening a stream and use csvreader class (like the link you posted) ? I guess the way described here is general and the other one supports CSV files, but what's the benefit of using csvReader class ? for what purposes csvReader is good for ?
I am afraid I can't answer it, but fell free to post a new question.

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.