我正在寻找Swift文件输入/输出操作,以便下周在我的大学进行简短演示。我真的没有发现太多。Apple Swift文件IO操作
下面是我找到的操作:
let location = "/Users/test/test.txt"
var str = "Hello, playground"
//write
str.writeToFile(location, atomically: false, encoding: NSUTF8StringEncoding, error: nil)
//read
let filecontent = String(contentsOfFile: location, encoding: NSUTF8StringEncoding, error: nil)
//binary read
let test = NSData(contentsOfFile: location)
//if file exists
NSFileManager().fileExistsAtPath(location)
//find Files in the App Bundle with suffix test and prefix txt
var path= NSBundle.mainBundle().pathForResource("test", ofType: "txt")
//path for dynamic user names
if let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] {
let path = dirs[0].stringByAppendingPathComponent("file.txt")
//printout: /Users/test/Documents/file.txt
是否有运营与我可以读取单个字符?
我可以将文本追加到文件吗?
我可以写/只读字符串吗?
有没有其他文件操作,我需要知道?
你的问题有点宽泛,但你应该看看NSInputStream和NSOutputStream。 – 2014-11-14 20:24:28
...还有NSFileHandle ... – 2014-11-14 21:08:59