From 42dd3d83828e0d2727736243745328ab84c67ff6 Mon Sep 17 00:00:00 2001
From: merzlovgleb <merzlovgleb@gmail.com>
Date: Sat, 14 Dec 2024 20:38:00 +0300
Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?=
 =?UTF-8?q?=D0=B2=D0=B0=D0=BB=20=D0=B4=D0=BE=D0=BC=D0=B0=D1=88=D0=BA=D1=83?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .idea/.name                                |  1 +
 edo1.iml                                   | 11 +++++
 src/Main.java                              | 51 ++++++++++++----------
 src/fileManagement/FileService.java        | 25 +++++++++++
 src/fileManagement/entity/FileService.java | 40 +++++++++++++++++
 5 files changed, 104 insertions(+), 24 deletions(-)
 create mode 100644 .idea/.name
 create mode 100644 edo1.iml
 create mode 100644 src/fileManagement/entity/FileService.java

diff --git a/.idea/.name b/.idea/.name
new file mode 100644
index 0000000..002da1d
--- /dev/null
+++ b/.idea/.name
@@ -0,0 +1 @@
+Main.java
\ No newline at end of file
diff --git a/edo1.iml b/edo1.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/edo1.iml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>
\ No newline at end of file
diff --git a/src/Main.java b/src/Main.java
index 34f2825..378373b 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -1,4 +1,5 @@
 import entities.User;
+import fileManagement.FileService;
 
 import java.io.File;
 import java.io.IOException;
@@ -69,31 +70,33 @@ public class Main {
 //        a == b
         s1.equals(s2);*/
 
-        Path pathToFolderUploads = Path.of("uploads");
-        if (!Files.exists(pathToFolderUploads)) {
-            new File("uploads").mkdir();
-        }
-
-        Path path = Path.of("uploads", "text.cs");
-
-        Path somePath = Path.of("src");
-
-        if (Files.isDirectory(somePath)) {
-            System.out.println(somePath.getName(0) + " директория");
-        }
-
-        try {
-            if (!Files.exists(path)) {
-                Files.createFile(path);
-            }
-        } catch (IOException e) {
-            System.out.println("произошла ошибка при работе с файлом");
-            throw new RuntimeException(e);
-        }
-
-        walkThroughDirectory(somePath, 0);
+//        Path pathToFolderUploads = Path.of("uploads");
+//        if (!Files.exists(pathToFolderUploads)) {
+//            new File("uploads").mkdir();
+//        }
+//
+//        Path path = Path.of("uploads", "text.cs");
+//
+//        Path somePath = Path.of("src");
+//
+//        if (Files.isDirectory(somePath)) {
+//            System.out.println(somePath.getName(0) + " директория");
+//        }
+//
+//        try {
+//            if (!Files.exists(path)) {
+//                Files.createFile(path);
+//            }
+//        } catch (IOException e) {
+//            System.out.println("произошла ошибка при работе с файлом");
+//            throw new RuntimeException(e);
+//        }
+//
+//        walkThroughDirectory(somePath, 0);
+        FileService fileService = new FileService();
+        fileService.printFile("text.exe");
 
 //        MAC/LINUX /Users/ivanberezuckij/work/it-park/lessons/edo;
 //        WINDA: C:\Users\ivanberezuckij\work\it-park\lessons\edo;
     }
-}
\ No newline at end of file
+}
diff --git a/src/fileManagement/FileService.java b/src/fileManagement/FileService.java
index 03122eb..37f493c 100644
--- a/src/fileManagement/FileService.java
+++ b/src/fileManagement/FileService.java
@@ -36,4 +36,29 @@ public class FileService {
         FileEntity savedFile = new FileEntity(pathToFile.getFileName(), owner, content, password);
 
     }
+
+    public void printFile(String fileName) {
+        Path pathToFile = Path.of(baseFilesPath, fileName);
+
+        try {
+            File targetFile = pathToFile.toFile();
+            if (!targetFile.exists()) {
+                throw new FileNotFoundException(String.format("Ошибка: файл %s не существует", targetFile.getAbsolutePath()));
+            }
+
+            FileInputStream fin = new FileInputStream(targetFile);
+            System.out.println("Содержимое файла " + targetFile.getAbsolutePath());
+
+            int c = 0;
+            while ((c = fin.read()) != -1) {
+                System.out.print((char) c);
+            }
+        } catch (FileNotFoundException e) {
+            System.out.println(e.getMessage());
+        } catch (IOException e) {
+            System.out.println("Ошибка чтения файла " + e.getMessage());
+        } catch (Exception e) {
+            System.out.println("Что-то пошло не так :)");
+        }
+    }
 }
diff --git a/src/fileManagement/entity/FileService.java b/src/fileManagement/entity/FileService.java
new file mode 100644
index 0000000..d8c86a9
--- /dev/null
+++ b/src/fileManagement/entity/FileService.java
@@ -0,0 +1,40 @@
+package fileManagement.entity;
+import fileManagement.entity.FileEntity;
+
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+// класс, описывающий use-case из нашей схемы
+public class FileService {
+    private String baseFilesPath = "uploads";
+
+    public void createFile(String fileName, String owner, String content, String password) {
+        Path pathToFile = Path.of(baseFilesPath, fileName);
+
+        Path createdFile = null;
+        try {
+            createdFile = Files.createFile(pathToFile);
+        } catch (IOException exception) {
+            System.out.println("Произошла ошибка при создании файла: " + exception.getMessage());
+            throw new IllegalArgumentException("Некорректный путь при создании файла");
+        }
+
+        File file = createdFile.toFile();
+
+        try (
+                OutputStreamWriter writer = new OutputStreamWriter(
+                        new FileOutputStream(file)
+                )
+        ) {
+            writer.write(content);
+        } catch (IOException e) {
+            System.out.println("Произошла ошибка при записи контента в файл: " + e.getMessage());
+        }
+
+        FileEntity savedFile = new FileEntity(pathToFile.getFileName(), owner, content, password);
+
+    }
+}
+
+
-- 
GitLab