如何在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) } }
广告