Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.net.URL;
- import java.nio.charset.Charset;
- import java.util.ArrayList;
- public class Main {
- public static void main(String[] args) throws IOException {
- ArrayList<String> arrayOfUrl = new ArrayList();
- int indexOfElementArray = 0;
- try (
- InputStream fileInput = new FileInputStream("empty.txt");
- InputStreamReader inputStreamRead = new InputStreamReader(fileInput, Charset.forName("UTF-8"));
- BufferedReader bufferRead = new BufferedReader(inputStreamRead);) {
- String url;
- while ((url = bufferRead.readLine()) != null) {
- arrayOfUrl.add(indexOfElementArray, url);
- indexOfElementArray++;
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- while (indexOfElementArray > 0) {
- indexOfElementArray--;
- saveImage(arrayOfUrl.get(indexOfElementArray), indexOfElementArray + ".jpg");
- }
- }
- public static void saveImage(String imageUrl, String destinationFile) throws IOException {
- URL url = new URL(imageUrl);
- InputStream is = url.openStream();
- OutputStream os = new FileOutputStream(destinationFile);
- byte[] b = new byte[2048];
- int length;
- while ((length = is.read(b)) != -1) {
- os.write(b, 0, length);
- }
- is.close();
- os.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement