From 0dc81c354fac1024720686463d0d718413cdb10f Mon Sep 17 00:00:00 2001 From: Jungpyo Hong <54448459+jphong1111@users.noreply.github.com> Date: Wed, 5 May 2021 12:18:53 -0500 Subject: [PATCH] Create FileManager.swift --- Helper/FileManageHandler/FileManager.swift | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Helper/FileManageHandler/FileManager.swift diff --git a/Helper/FileManageHandler/FileManager.swift b/Helper/FileManageHandler/FileManager.swift new file mode 100644 index 0000000..f4223b1 --- /dev/null +++ b/Helper/FileManageHandler/FileManager.swift @@ -0,0 +1,79 @@ +// +// FileManager.swift +// VariousManagerDemoApp +// +// Created by JungpyoHong on 4/22/21. +// +import Foundation + +class FileManager { + + static let manager = FileManageHelper() + + let useFileHandler = FileManager.default + + private init() {} + + func getDocumentDirectoryPath() -> URL { + guard let url = useFileHandler.urls(for: .documentDirectory, + in: .userDomainMask).first else { fatalError("Can't find path") } + return url + } + + func createFile(resourceName: String, fileExtension: String) { + let fileURLProject = Bundle.main.path(forResource: resourceName, ofType: fileExtension) + let folderUrl = getDocumentDirectoryPath() + let fileUrl = folderUrl.appendingPathComponent("\(resourceName).\(fileExtension)") + do { + try useFileHandler.copyItem(atPath: fileURLProject ?? "", toPath: fileUrl.path) + } catch { + print(error) + } + } + + func writeFile(fileName: String, fileType: String, text: String) { + let fileNameWithPath: String = "\(fileName).\(fileType)" + let filePath = getDocumentDirectoryPath() + let fileUrl = filePath.appendingPathComponent(fileNameWithPath) + if useFileHandler.fileExists(atPath: fileUrl.path) { + do { + guard let fileContents = useFileHandler.contents(atPath: fileUrl.path) else { return } + let fileContentsAsString = String(bytes: fileContents, encoding: .utf8) + let fullText = "\(String(describing: fileContentsAsString) ):\(text)" + guard let data = fullText.data(using: .utf8) else { return } + try data.write(to: fileUrl) + } catch { + print(error) + } + } else { + print("File is not existed") + } + } + + func readFile(filename: String, type: String) -> String { + let realFileName = "\(filename).\(type)" + let fileURLProject = getDocumentDirectoryPath() + + let fileURL = fileURLProject.appendingPathComponent(realFileName) + + do { + let fileContentsAsString = try String(contentsOf: fileURL, encoding: .utf8) + return fileContentsAsString + } catch { fatalError("no file") } + } + + func removeFile(fileName: String, fileType: String) { + let fileNameWithPath: String = "\(fileName).\(fileType)" + let filePath = getDocumentDirectoryPath() + let fileUrl = filePath.appendingPathComponent(fileNameWithPath) + if useFileHandler.fileExists(atPath: fileUrl.path) { + do { + try useFileHandler.removeItem(at: fileUrl) + } catch { + print(error) + } + } else { + print("File is not existed") + } + } +}