Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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();
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);
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
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);
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.