Note that there are some explanatory texts on larger screens.

plurals
  1. POdownloading images from reddit with java errors
    primarykey
    data
    text
    <p>So I have been googling how to download images and not getting very well written explanations, just code examples. I don't completely understand some of this code, mainly with the</p> <pre><code> for (int b; (b = is.read()) != -1;) { os.write(b); } </code></pre> <p>Can someone explain this above code like I am five, and also any alternatives to this method.</p> <p>EDIT2</p> <pre><code>import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class Downloader { static String path = "C:\\reddit\\"; public static void main(String[] args) { connect(); } private static void download(String imageURL, int i) { InputStream is = null; OutputStream os = null; try { URL url = new URL(imageURL); is = url.openStream(); os = new FileOutputStream(path + i + ".jpg"); for (int b; (b = is.read()) != -1;) { os.write(b); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void connect() { try { Document doc = Jsoup.connect("http://www.reddit.com/r/pics").get(); Elements url = doc.select("a"); int i = 0; for (Element img : url) { if (img.attr("href").startsWith("http://imgur.com/")) { String image = img.attr("abs:href")+".jpg"; System.out.println(image); i++; System.out.println(i); download(image, i); } } } catch (IOException e) { System.out.println("page scrape fail"); } } </code></pre> <p>}</p> <p>EDIT I noticed my output is not right, its writing duplicates, I will just post my console result</p> <pre><code>http://imgur.com/f7rW2Of 1 http://imgur.com/f7rW2Of 2 http://imgur.com/35jpkez 3 http://imgur.com/35jpkez 4 http://imgur.com/IX9HMJG 5 http://imgur.com/IX9HMJG 6 http://imgur.com/B6MoDbT 7 http://imgur.com/B6MoDbT 8 http://imgur.com/XMtCUY9 9 http://imgur.com/XMtCUY9 10 http://imgur.com/UkbbiBl 11 http://imgur.com/UkbbiBl 12 http://imgur.com/YfLsCal 13 http://imgur.com/YfLsCal 14 http://imgur.com/9Q3CJtT 15 http://imgur.com/9Q3CJtT 16 http://imgur.com/Vt7sWTf 17 http://imgur.com/Vt7sWTf 18 http://imgur.com/hBUH5kS 19 http://imgur.com/hBUH5kS 20 http://imgur.com/gallery/OWQH0h6 21 http://imgur.com/gallery/OWQH0h6 22 http://imgur.com/a/hiJXI 23 http://imgur.com/a/hiJXI 24 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload