/
itokar98
/
HomeWorkCoreFileTask1
Обзор
Документация
Войти
/
itokar98
/
HomeWorkCoreFileTask1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/Main.java
120 строк
5 KB
Ilya Tokar
First commit
13 окт 2025, 03:57
13 окт 2025, 03:57
45be2a2
Код
Авторство
О чём код?
import java.io.File; import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) { // Базовый путь - измените на ваш реальный путь // String basePath = "D://Games"; // Для Windows // String basePath = "/Users/admin/Games"; // Для macOS String basePath = "/home/ilyatokar/Games"; // Для Linux StringBuilder log = new StringBuilder(); try { // 1. Создаем основные директории в папке Games File srcDir = createDirectory(basePath + "/src", log); File resDir = createDirectory(basePath + "/res", log); File savegamesDir = createDirectory(basePath + "/savegames", log); File tempDir = createDirectory(basePath + "/temp", log); // 2. Создаем поддиректории в src if (srcDir != null) { File mainDir = createDirectory(srcDir.getPath() + "/main", log); File testDir = createDirectory(srcDir.getPath() + "/test", log); // 3. Создаем файлы в подкаталоге main if (mainDir != null) { createFile(mainDir.getPath() + "/Main.java", log); createFile(mainDir.getPath() + "/Utils.java", log); } } // 4. Создаем директории в res if (resDir != null) { createDirectory(resDir.getPath() + "/drawables", log); createDirectory(resDir.getPath() + "/vectors", log); createDirectory(resDir.getPath() + "/icons", log); } // 5. Создаем файл temp.txt в директории temp if (tempDir != null) { createFile(tempDir.getPath() + "/temp.txt", log); } // Записываем лог в файл temp.txt writeLogToFile(basePath + "/temp/temp.txt", log.toString()); System.out.println("Установка завершена!"); System.out.println("Лог установки:"); System.out.println(log.toString()); } catch (Exception e) { System.err.println("Произошла ошибка во время установки: " + e.getMessage()); e.printStackTrace(); } } /** * Метод для создания директории * @param path путь к директории * @param log StringBuilder для записи лога * @return объект File созданной директории или null при ошибке */ private static File createDirectory(String path, StringBuilder log) { File dir = new File(path); if (dir.mkdir()) { String message = "Директория создана: " + path; log.append(message).append("\n"); System.out.println(message); return dir; } else { String message = "Ошибка создания директории: " + path; log.append(message).append("\n"); System.err.println(message); return null; } } /** * Метод для создания файла * @param path путь к файлу * @param log StringBuilder для записи лога * @return объект File созданного файла или null при ошибке */ private static File createFile(String path, StringBuilder log) { File file = new File(path); try { if (file.createNewFile()) { String message = "Файл создан: " + path; log.append(message).append("\n"); System.out.println(message); return file; } else { String message = "Ошибка создания файла (возможно, уже существует): " + path; log.append(message).append("\n"); System.err.println(message); return null; } } catch (IOException e) { String message = "Ошибка ввода-вывода при создании файла " + path + ": " + e.getMessage(); log.append(message).append("\n"); System.err.println(message); return null; } } /** * Метод для записи лога в файл * @param filePath путь к файлу лога * @param logContent содержимое лога */ private static void writeLogToFile(String filePath, String logContent) { try (FileWriter writer = new FileWriter(filePath)) { writer.write(logContent); System.out.println("Лог записан в файл: " + filePath); } catch (IOException e) { System.err.println("Ошибка при записи лога в файл: " + e.getMessage()); } } }