0

I am trying to read a CSV file from the web in Java but I only need the first line of the CSV file. That is, I don't really need to download the entire CSV file. How can I do this efficiently? For instance, suppose the CSV is 100mb but I only need the first row which is less than 1kb.

Here's what I am using to download a CSV:

try {

    String str = "https://www.websitelink.csv"; 
    BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(str).openStream()));

    String line = reader.readLine(); //this is the only line I need (the first line)

} catch (Exception e1) {
    e1.printStackTrace();
}

Note, my question is not asking how to read only the first line, it is asking how to download only the first line of a CSV.

CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • 1
    This is quite different. I am talking about *downloading* only the first line of a CSV, not reading it. I have demonstrated in the code above that I already know how to only read the first line. – CodeGuy Jan 03 '18 at 19:21
  • How about [this answer](https://stackoverflow.com/questions/17643851/downloading-a-portion-of-a-file-using-http-requests/17644522#17644522) to a similar question on SO titled "Downloading a portion of a File using HTTP Requests"? – skomisa Jan 03 '18 at 20:52
  • It is not "quite different", if you read a stream over HTTP, and you stop reading when you have what you want, then you don't need to download the entire file. – Mark Rotteveel Jan 03 '18 at 21:03

0 Answers0