Skip to content

Instantly share code, notes, and snippets.

@hcn1519
Last active July 15, 2017 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hcn1519/05f6088279331452fedffdf0742c0f62 to your computer and use it in GitHub Desktop.
Save hcn1519/05f6088279331452fedffdf0742c0f62 to your computer and use it in GitHub Desktop.
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