Last active
July 9, 2018 14:28
-
-
Save steklopod/a518a9b6bf845b53487252369489d373 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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