如何在iOS上建立檔案、寫入資料和讀取資料?
作為軟體開發者,我們應該始終了解如何操作檔案,向檔案寫入資料,從檔案讀取資料等等。
在這篇文章中,我們將學習如何建立檔案,向檔案寫入資料,然後從同一檔案中讀取資料。
讓我們開始吧!
步驟 1 − 建立新的 Xcode 專案 → 單檢視應用程式 → 將其命名為“ReadingWritingFile”
步驟 2 − 開啟 ViewController.swift 並新增如下所示的新函式
public func createAndWriteFile() {
}現在我們將建立一個檔案並列印檔案的路徑。
步驟 3 − 在 createAndWriteFile 函式中新增
let fileName = "sample"
let documentDirectoryUrl = try! FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true
)
let fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt")
// prints the file path
print("File path \(fileUrl.path)")現在你的 createAndWriteFile 函式應該如下所示:
public func createAndWriteFile() {
let fileName = "sample"
let documentDirectoryUrl = try! FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true
)
let fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt")
// prints the file path
print("File path \(fileUrl.path)")
//data to write in file.
let stringData = "Hello Tutorials Point"
do {
try stringData.write(to: fileUrl, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print (error)
}
}現在我們將向檔案寫入資料。
將以下程式碼新增到現有函式中
//data to write in file.
let stringData = "Hello Tutorials Point"
do {
try stringData.write(to: fileUrl, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print (error)
}步驟 4 − 你的最終函式應該如下所示:
// function to create file and write into the same.
public func createAndWriteFile() {
let fileName = "sample"
let documentDirectoryUrl = try! FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true
)
let fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt")
// prints the file path
print("File path \(fileUrl.path)")
//data to write in file.
let stringData = "Hello Tutorials Point"
do {
try stringData.write(to: fileUrl, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print (error)
}
}步驟 5 − 透過從 viewDidLoad() 呼叫新方法來執行專案,然後導航到檔案路徑並驗證內容。
步驟 6 − 現在我們將讀取內容,將以下程式碼複製到同一函式中
var readFile = ""
do {
readFile = try String(contentsOf: fileUrl)
} catch let error as NSError {
print(error)
}
print (readFile)搞定了!

步驟 7 − 完成程式碼:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.createReadAndWriteFile()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// function to create file and write into the same.
public func createReadAndWriteFile() {
let fileName = "sample"
let documentDirectoryUrl = try! FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true
)
let fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt")
// prints the file path
print("File path \(fileUrl.path)")
//data to write in file.
let stringData = "Hello Tutorials Point."
do {
try stringData.write(to: fileUrl, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print (error)
}
var readFile = ""
do {
readFile = try String(contentsOf: fileUrl)
} catch let error as NSError {
print(error)
}
print (readFile)
}
}
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP