附加文本或数​​据文本文件中,斯威夫特 [英] Append text or data to text file in Swift

查看:106
本文介绍了附加文本或数​​据文本文件中,斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读读取及写入文本文件数据

我需要的数据(字符串)附加到我的文本文件的末尾。结果
一个显而易见的方法来做到这一点是从磁盘中读取文件并追加字符串结束它并把它写回,但它是有效的,特别是如果你正在处理大量文件,并在经常做的事情。

所以现在的问题是如何追加字符串到一个文本文件的末尾,不读文件,写了整个事情回来?

到目前为止,我有:

 让DIR:NSURL = NSFileManager.defaultManager()URLsForDirectory(NSSearchPathDirectory.CachesDirectory,inDomains:NSSearchPathDomainMask.UserDomainMask)。去年作为NSURL
    让fileurl = dir.URLByAppendingPathComponent(log.txt中)
    VAR错误:NSError?
    //直到我们找到一个方法来追加东西文件
    如果让current_content_of_file =的NSString(contentsOfURL:fileurl,编码:NSUTF8StringEncoding,错误:&放大器; ERR){
        \\(current_content_of_file)\\ n \\(NSDate的()) - > \\(对象)writeToURL(fileurl,原子:真的,编码:NSUTF8StringEncoding,错误:&放大器; ERR)。
    }其他{
        。\\(NSDate的()) - > \\(对象)writeToURL(fileurl,原子:真的,编码:NSUTF8StringEncoding,错误:&放大器; ERR)
    }
    如果犯错!= {为零
        的println(CAN NOT LOG:\\(ERR))
    }


解决方案

您应该使用NSFileHandle,它可以seek该文件的结尾

 让DIR:NSURL = NSFileManager.defaultManager()URLsForDirectory(NSSearchPathDirectory.CachesDirectory,inDomains:NSSearchPathDomainMask.UserDomainMask)。去年作为NSURL
让fileurl = dir.URLByAppendingPathComponent(log.txt中)让字符串=\\(NSDate的())\\ n
让数据= string.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion:假的)!如果NSFileManager.defaultManager()。fileExistsAtPath(fileurl.path!){
    VAR错误:NSError?
    如果让文件句柄= NSFileHandle(forWritingToURL:fileurl,错误:&放大器; ERR){
        fileHandle.seekToEndOfFile()
        fileHandle.writeData(数据)
        fileHandle.closeFile()
    }
    其他{
        的println(无法打开文件句柄\\(ERR))
    }
}
其他{
    VAR错误:NSError?
    如果data.writeToURL(fileurl,选择:.DataWritingAtomic,错误:&放大器; ERR)!{
        的println(不能写\\(ERR))
    }
}

I already have read Read and write data from text file

I need to append the data (a string) to the end of my text file.
One obvious way to do it is to read the file from disk and append the string to the end of it and write it back, but it is not efficient, especially if you are dealing with large files and doing in often.

So the question is "How to append string to the end of a text file, without reading the file and writing the whole thing back"?

so far I have:

    let dir:NSURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.CachesDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last as NSURL
    let fileurl =  dir.URLByAppendingPathComponent("log.txt")
    var err:NSError?
    // until we find a way to append stuff to files
    if let current_content_of_file = NSString(contentsOfURL: fileurl, encoding: NSUTF8StringEncoding, error: &err) {
        "\(current_content_of_file)\n\(NSDate()) -> \(object)".writeToURL(fileurl, atomically: true, encoding: NSUTF8StringEncoding, error: &err)
    }else {
        "\(NSDate()) -> \(object)".writeToURL(fileurl, atomically: true, encoding: NSUTF8StringEncoding, error: &err)
    }
    if err != nil{
        println("CANNOT LOG: \(err)")
    }

解决方案

You should use NSFileHandle, it can seek to the end of the file

let dir:NSURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.CachesDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last as NSURL
let fileurl =  dir.URLByAppendingPathComponent("log.txt")

let string = "\(NSDate())\n"
let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!

if NSFileManager.defaultManager().fileExistsAtPath(fileurl.path!) {
    var err:NSError?
    if let fileHandle = NSFileHandle(forWritingToURL: fileurl, error: &err) {
        fileHandle.seekToEndOfFile()
        fileHandle.writeData(data)
        fileHandle.closeFile()
    }
    else {
        println("Can't open fileHandle \(err)")
    }
}
else {
    var err:NSError?
    if !data.writeToURL(fileurl, options: .DataWritingAtomic, error: &err) {
        println("Can't write \(err)")
    }
}

这篇关于附加文本或数​​据文本文件中,斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆