Здравствуйте.
Код отрабатывает без ошибок, но валидацию не проходит. Подскажите, что не так.
UPD: вместо Path path = Paths.get(downloadDirectory.toString() + fileName); стоит Path path = Paths.get(downloadDirectory + "\\" + fileName); Почему то не тот вариант прикрепился.
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);
String fileName = new File(urlString).getName();
Path tempFile = Files.createTempFile("temp-", ".tmp");
Files.copy(url.openStream(), tempFile, StandardCopyOption.REPLACE_EXISTING);
Path path = Paths.get(downloadDirectory.toString() + fileName);
if (!Files.exists(downloadDirectory))
Files.createDirectories(downloadDirectory);
Files.move(tempFile, path, StandardCopyOption.REPLACE_EXISTING);
return path;
}
}