FileManager Basics
FileManager is Swift's helper for finding, creating, and reading files on the computer, like a librarian who knows where everything is kept.
In this page:
Syntax
import Foundation
let url = FileManager.default.temporaryDirectory.appendingPathComponent("file.txt")
try "text".write(to: url, atomically: true, encoding: .utf8)
let text = try String(contentsOf: url, encoding: .utf8)
FileManager.default.fileExists(atPath: url.path)
Writing and Reading a File
A String can be written directly to a file URL, and later read back using the matching initializer, both of which can throw if something goes wrong.
Example: Writing and Reading a File
// Import the `Foundation` module
import Foundation
// Declare a constant `tempDir`, set to `FileManager.default.temporaryDirectory`
let tempDir = FileManager.default.temporaryDirectory
// Declare a constant `fileURL`, set to `tempDir.appendingPathComponent("swift_demo.txt")`
let fileURL = tempDir.appendingPathComponent("swift_demo.txt")
try! "Hello from Swift".write(to: fileURL, atomically: true, encoding: .utf8)
// Declare a constant `contents`, set to `try! String(contentsOf: fileURL, encoding: .utf8)`
let contents = try! String(contentsOf: fileURL, encoding: .utf8)
// Print `contents` to the console
print(contents)
Login to try C/C++/Java/PHP code in the editor
Checking If a File Exists
FileManager.default.fileExists(atPath:) checks whether a given path currently points to an existing file or directory.
Example: Checking If a File Exists
// Import the `Foundation` module
import Foundation
// Declare a constant `tempDir`, set to `FileManager.default.temporaryDirectory`
let tempDir = FileManager.default.temporaryDirectory
// Declare a constant `fileURL`, set to `tempDir.appendingPathComponent("check_me.txt")`
let fileURL = tempDir.appendingPathComponent("check_me.txt")
// Print "Exists before write: \(FileManager.default.fileExists(atPath: fileURL.path))" to the console
print("Exists before write: \(FileManager.default.fileExists(atPath: fileURL.path))")
try! "data".write(to: fileURL, atomically: true, encoding: .utf8)
// Print "Exists after write: \(FileManager.default.fileExists(atPath: fileURL.path))" to the console
print("Exists after write: \(FileManager.default.fileExists(atPath: fileURL.path))")
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Forgetting
import Foundationis required beforeFileManageris available. - Assuming a hardcoded file path will exist across every machine or sandbox; using a temporary directory is safer for portable examples.
- Forgetting many
FileManageroperations, like writing or reading a file's contents, can throw and need to be wrapped intry/do-catch.
Chapter Summary
FileManager.defaultprovides access to file-system operations like checking existence, creating, and deleting files.FileManager.default.temporaryDirectorygives a safe, writable location for scratch files.- Writing and reading file contents uses
String(contentsOf:)/.write(to:...), which can throw errors. .fileExists(atPath:)checks whether a given path currently exists.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: