Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Foundation
let fileManager = FileManager()
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let dataPath = documentsDirectory.appendingPathComponent("FileManager Directory")
do {
try fileManager.createDirectory(atPath: dataPath.path, withIntermediateDirectories: false, attributes: nil)
} catch let error as NSError {
print("Error creating directory: \(error.localizedDescription)")
}
do {
let helloPath = dataPath.appendingPathComponent("Hello.txt")
let text = "Hello File From Swift"
do {
try text.write(to: helloPath, atomically: false, encoding: .utf8)
}
} catch let error as NSError {
print("Error creating File : \(error.localizedDescription)")
}
do {
let helloPath = dataPath.appendingPathComponent("Hello.txt")
let text2 = try String(contentsOf: helloPath, encoding: .utf8)
print(text2)
}
catch let error as NSError {
print("Error creating File : \(error.localizedDescription)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment