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


}
}
Niminim
  • 668
  • 1
  • 7
  • 16
  • Download that file onto local manually and then use filereader. – SMA Jul 31 '16 at 09: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 ? – Niminim Jul 31 '16 at 09:53
  • 1
    Then better you read from url or download the file and then read the file. – SMA Jul 31 '16 at 09:57
  • @SMA can you add the relevant code to do one of the options (reading from a url or downloading the file) ? – Niminim Jul 31 '16 at 10:06
  • @SMA I managed to read the file from the URL.Thanks! God bless the internet :) – Niminim Jul 31 '16 at 10:16

1 Answers1

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

}
Community
  • 1
  • 1
c0der
  • 18,467
  • 6
  • 33
  • 65
  • Yeah, I know. I used that code and changed it a bit. – Niminim Jul 31 '16 at 13:02
  • 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 ? – Niminim Jul 31 '16 at 13:07
  • I am afraid I can't answer it, but fell free to post a new question. – c0der Jul 31 '16 at 17:26