/
spirzen
/
it-code-examples
Обзор
Документация
Войти
/
spirzen
/
it-code-examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/java/java-503-131-008/main.java
40 строк
2 KB
Spirzen
Code migration pack
09 июн 2026, 01:41
09 июн 2026, 01:41
cebc284
Код
Авторство
О чём код?
import java.io.IOException; import java.nio.file.*; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class BackupUtility { public static void main(String[] args) { Path sourceDir = Paths.get("source_data"); Path backupDir = Paths.get("backup_data"); try { // Создание директории бэкапа, если её нет Files.createDirectories(backupDir); LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); String timestamp = now.format(formatter); // Поиск всех файлов в источнике try (var stream = Files.list(sourceDir)) { stream.forEach(file -> { try { String fileName = file.getFileName().toString(); String nameWithoutExt = fileName.substring(0, fileName.lastIndexOf('.')); String ext = fileName.substring(fileName.lastIndexOf('.')); Path newPath = backupDir.resolve(nameWithoutExt + "_" + timestamp + ext); Files.copy(file, newPath, StandardCopyOption.REPLACE_EXISTING); System.out.println("Скопировано: " + fileName + " -> " + newPath.getFileName()); } catch (Exception e) { System.err.println("Ошибка копирования " + file + ": " + e.getMessage()); } }); } } catch (IOException e) { System.err.println("Критическая ошибка: " + e.getMessage()); } } }