Что ему не нравится?
package com.javarush.task.task31.task3112;
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://javarush.ru/testdata/secretPasswords.txt", Paths.get("D:/MyDownloads"));
//Path passwords = downloadFile("https://javarush.ru/testdata/secretPasswords.txt", Paths.get("/Users/mac/Documents/JRTasks/"));
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);
String fileName = url.toString().substring(urlString.lastIndexOf("/") + 1); //получаем имя скаченного файла из ссылки, а именно secretPasswords.txt
InputStream inStream = url.openStream();
Path tempFile = Files.createTempFile("temp-", ".tmp"); //создаем временный файл temp--751817858655870895.tmp
Files.copy(inStream, tempFile); //скачиваем содержимое secretPasswords.txt в temp--751817858655870895.tmp
Path downloadedFile = Files.move(tempFile, Paths.get(downloadDirectory + "//" + tempFile.getFileName().toString())); //перемещаем temp--751817858655870895.tmp в указанную директорию
String fileDirectory = downloadedFile + "//" + fileName; //создаем путь для файла с именем secretPasswords.txt
Path resultFile = Files.createFile(Paths.get(fileDirectory)); // создаем файл из этого пути
Files.copy(downloadedFile, resultFile); //копируем содержимое перемещенного temp--751817858655870895.tmp в созданный файл secretPasswords.txt
return resultFile;
}
}
// /Users/mac/Documents/JRTasks/allFilesContent.txt
//Path passwords = downloadFile("https://javarush.ru/testdata/secretPasswords.txt", Paths.get("/Users/mac/Documents/JRTasks/"));
// Path downloadedFile = Files.move(tempFile, Paths.get(downloadDirectory + tempFile.getFileName().toString()));