← Back to Swift Course | Chapter 14: Standard Library & Best Practices | Lesson 4 of 7

FileManager के Basics

FileManager computer पर files ढूंढने, बनाने, और पढ़ने के लिए Swift का helper है, एक librarian जैसा जो जानता है सब कुछ कहां रखा है।
Syntax
swift
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

markup
// 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)

Checking If a File Exists

FileManager.default.fileExists(atPath:) जांचता है कि एक दिया path अभी किसी मौजूदा file या directory की ओर point करता है या नहीं।

उदाहरण: Checking If a File Exists

markup
// 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))")
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. #}
आम गलतियां
  1. यह भूल जाना कि FileManager के available होने से पहले import Foundation ज़रूरी है।
  2. यह मान लेना कि एक hardcoded file path हर machine या sandbox में मौजूद होगा; portable examples के लिए एक temporary directory उपयोग करना safer है।
  3. यह भूल जाना कि कई FileManager operations, जैसे किसी file की contents लिखना या पढ़ना, throw कर सकते हैं और try/do-catch में wrapped होने चाहिए।
चैप्टर सारांश
  • FileManager.default file-system operations तक access देता है जैसे existence जांचना, files बनाना, और delete करना।
  • FileManager.default.temporaryDirectory scratch 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.