Skip to content

Instantly share code, notes, and snippets.

@steklopod
Last active July 9, 2018 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steklopod/a518a9b6bf845b53487252369489d373 to your computer and use it in GitHub Desktop.
Save steklopod/a518a9b6bf845b53487252369489d373 to your computer and use it in GitHub Desktop.
package com.javarush.task.task31.task3112;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/*
Загрузчик файлов
*/
public class Solution {
public static void main(String[] args) throws IOException {
Path passwords = downloadFile("https://www.amigo.com/ship/secretPassword.txt", Paths.get("D:/MyDownloads"));
for (String line : Files.readAllLines(passwords)) {
System.out.println(line);
}
}
public static Path downloadFile(String urlString, Path downloadDirectory) throws IOException {
URL url = new URL(urlString);
InputStream inputStream = url.openStream();
Path tmp = Files.createTempFile("temp-", ".tmp");
Files.copy(inputStream, tmp);
String fileName = urlString.substring(urlString.lastIndexOf("/"));
Path destPath = Paths.get(downloadDirectory + "/" + fileName);
Files.move(tmp, destPath);
return destPath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment