0

I am trying to read the csv file from the URL. But I get java.io.FileNotFoundException. The URL works on browser. Please help. Below is my csv reading code.

public static void main(String args[]) {


        InputStream input = null;

        try {
            input = new URL("http://prod2.riskval.com/site/table.csv​").openStream();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Reader reader = new InputStreamReader(input, "UTF-8");
            BufferedReader br = new BufferedReader(reader);
            String data;
            try {
                data = br.readLine();
                System.out.println(data);
            } catch (IOException e) {
                e.printStackTrace();
            }

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

}
Kristen Chang
  • 21
  • 1
  • 5

3 Answers3

0

The file seems to be the problem. I tried with another file and it worked like this.

public static void main(String args[]) throws IOException {

InputStream input = null;

URL url = new URL("http://samplecsvs.s3.amazonaws.com/SalesJan2009.csv");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

String data;

data = in.readLine();
System.out.println(data);

}

BadChanneler
  • 1,450
  • 1
  • 12
  • 20
0

The reason is that your URL has hidden character, I just tried copy pasting the same URL in the browser and it gave me 404. as per this, when you copy the text from MS Word or WordPad or similar editor your get these %E2%80%8B characters which indicated end of line.

Just copy paste the URL in sublime or any other text editor which show hidden characters and you should see something like this

http://prod2.riskval.com/site/table.csv%E2%80%8B

Community
  • 1
  • 1
Deendayal Garg
  • 5,030
  • 2
  • 19
  • 33
0

This should solve your read problem. Please study it.

package tttttttttttttttt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;

public class ss {
    public static void main(String args[]) {

        InputStream input = null;
        BufferedReader br = null;
        StringBuilder body = new StringBuilder();
        String line = null;
        try {
            input = new URL("http://www.google.com").openStream();
            System.err.println(input);
            System.err.println(input.available());
            System.err.println(input.markSupported());
            br = new BufferedReader(new InputStreamReader(input));
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.print("Captured " + br);
    }
}
MVG
  • 36
  • 3