Swift-এ ফাইল তৈরি, পড়া এবং লেখা
Swift-এ ফাইল পরিচালনা করতে FileManager এবং Data ক্লাস ব্যবহার করা হয়। এখানে ফাইল তৈরি, পড়া, এবং লেখার পদ্ধতি দেখানো হলো:
১. ফাইল তৈরি এবং লেখা
import Foundation
let text = "Hello, World!"
let fileName = "example.txt"
let filePath = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
do {
try text.write(to: filePath, atomically: true, encoding: .utf8)
print("File created successfully.")
} catch {
print("Error creating file: \(error)")
}
২. ফাইল পড়া
do {
let readText = try String(contentsOf: filePath, encoding: .utf8)
print("File content: \(readText)")
} catch {
print("Error reading file: \(error)")
}
সংক্ষেপে
এই পদ্ধতিগুলোর মাধ্যমে Swift-এ সহজেই ফাইল তৈরি, পড়া, এবং লেখা যায়। FileManager ফাইলের অবস্থান নিয়ন্ত্রণ করে এবং Data ফাইলের কনটেন্ট ম্যানেজ করে।
Content added By