Ваши идеи...
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;
import java.nio.file.StandardCopyOption;
/*
Загрузчик файлов
*/
public class Solution {
public static void main(String[] args) throws IOException {
Path passwords = downloadFile("https://javarush.ru/testdata/secretPasswords.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 {
// implement this method
URL url = new URL(urlString);
InputStream inpStrUrl = url.openStream();
String[] urlName = urlString.split("/");
String[] name = urlName[urlName.length - 1].split("\\.");
Path tempFile = Files.createTempFile(name[0], "." + name[name.length - 1]);
Files.copy(inpStrUrl, tempFile, StandardCopyOption.REPLACE_EXISTING);
File newFile = new File(downloadDirectory + "\\" + tempFile.getFileName());
Files.move(tempFile, newFile.toPath());
File newFileName = new File(downloadDirectory + "\\" + name[0] + "." + name[name.length - 1]);
new File(downloadDirectory + "\\" + tempFile.getFileName()).renameTo(newFileName);
return newFileName.toPath();
}
}