file-io – 从文本文件读取和写入数据

前端之家收集整理的这篇文章主要介绍了file-io – 从文本文件读取和写入数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要读/写一个文本文件的数据,但我还没有能够弄清楚如何。

我在Swift的iBook中找到了这个示例代码,但我还是不知道如何写或读数据。

  1. import Cocoa
  2.  
  3. class DataImporter
  4. {
  5. /*
  6. DataImporter is a class to import data from an external file.
  7. The class is assumed to take a non-trivial amount of time to initialize.
  8. */
  9. var fileName = "data.txt"
  10. // the DataImporter class would provide data importing functionality here
  11. }
  12.  
  13. class DataManager
  14. {
  15. @lazy var importer = DataImporter()
  16. var data = String[]()
  17. // the DataManager class would provide data management functionality here
  18. }
  19.  
  20. let manager = DataManager()
  21. manager.data += "Some data"
  22. manager.data += "Some more data"
  23. // the DataImporter instance for the importer property has not yet been created”
  24.  
  25. println(manager.importer.fileName)
  26. // the DataImporter instance for the importer property has now been created
  27. // prints "data.txt”
  28.  
  29.  
  30.  
  31. var str = "Hello World in Swift Language."
对于读写,应该使用可写的位置,例如文档目录。以下代码显示如何读取和写入一个简单的字符串。你可以在操场上测试它。

Swift 1.x

  1. let file = "file.txt"
  2.  
  3. if let dirs : [String] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask,true) as? [String] {
  4. let dir = dirs[0] //documents directory
  5. let path = dir.stringByAppendingPathComponent(file);
  6. let text = "some text"
  7.  
  8. //writing
  9. text.writeToFile(path,atomically: false,encoding: NSUTF8StringEncoding,error: nil);
  10.  
  11. //reading
  12. let text2 = String(contentsOfFile: path,error: nil)
  13. }

Swift 2.2

  1. let file = "file.txt" //this is the file. we will write to and read from it
  2.  
  3. let text = "some text" //just a text
  4.  
  5. if let dir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,true).first {
  6. let path = NSURL(fileURLWithPath: dir).URLByAppendingPathComponent(file)
  7.  
  8. //writing
  9. do {
  10. try text.writeToURL(path,encoding: NSUTF8StringEncoding)
  11. }
  12. catch {/* error handling here */}
  13.  
  14. //reading
  15. do {
  16. let text2 = try NSString(contentsOfURL: path,encoding: NSUTF8StringEncoding)
  17. }
  18. catch {/* error handling here */}
  19. }

Swift 3.0

  1. let file = "file.txt" //this is the file. we will write to and read from it
  2.  
  3. let text = "some text" //just a text
  4.  
  5. if let dir = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask).first {
  6.  
  7. let path = dir.appendingPathComponent(file)
  8.  
  9. //writing
  10. do {
  11. try text.write(to: path,encoding: String.Encoding.utf8)
  12. }
  13. catch {/* error handling here */}
  14.  
  15. //reading
  16. do {
  17. let text2 = try String(contentsOf: path,encoding: String.Encoding.utf8)
  18. }
  19. catch {/* error handling here */}
  20. }

猜你在找的Swift相关文章