FileManager के Basics
FileManager computer पर files ढूंढने, बनाने, और पढ़ने के लिए Swift का helper है, एक librarian जैसा जो जानता है सब कुछ कहां रखा है।
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
एक String को सीधे एक file URL पर लिखा जा सकता है, और बाद में matching initializer उपयोग करके वापस पढ़ा जा सकता है, दोनों कुछ गलत होने पर throw कर सकते हैं।
उदाहरण: 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:) जांचता है कि एक दिया path अभी किसी मौजूदा file या directory की ओर point करता है या नहीं।
उदाहरण: 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. #}
आम गलतियां
- यह भूल जाना कि
FileManagerके available होने से पहलेimport Foundationज़रूरी है। - यह मान लेना कि एक hardcoded file path हर machine या sandbox में मौजूद होगा; portable examples के लिए एक temporary directory उपयोग करना safer है।
- यह भूल जाना कि कई
FileManageroperations, जैसे किसी file की contents लिखना या पढ़ना, throw कर सकते हैं औरtry/do-catchमें wrapped होने चाहिए।
चैप्टर सारांश
FileManager.defaultfile-system operations तक access देता है जैसे existence जांचना, files बनाना, और delete करना।FileManager.default.temporaryDirectoryscratch files के लिए एक safe, writable location देता है।- File contents लिखना और पढ़ना
String(contentsOf:)/.write(to:...)उपयोग करता है, जो errors throw कर सकते हैं। .fileExists(atPath:)जांचता है कि एक दिया path अभी मौजूद है या नहीं।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: