swift3.0 把字符串添加到文件结尾

//把字符串写进文件,文件保存在沙盒中Documents路径中

func writeStrToFile(receivedString: NSString) {

        

    let str = receivedString as String

    let wr = NSMutableData()

 wr.append(str.data(using:String.Encoding(rawValue: String.Encoding.utf8.rawValue))!)

        

    let manager = FileManager.default

  let urlsForDocDirectory = manager.urls(for:.documentDirectory, in:.userDomainMask)

    let docPath = urlsForDocDirectory[0]

    let file = docPath.appendingPathComponent("Demo.txt")

    let writeHandler = try? FileHandle(forWritingTo:file)

    if writeHandler != nil {

         writeHandler!.seekToEndOfFile()

         writeHandler?.write(wr as Data)

    }

        

        

}


阅读更多
版权声明:本文为博主原创文章,转载请说明出处:http://blog.csdn.net/amberoot https://blog.csdn.net/amberoot/article/details/79957400
文章标签: iOS  swift FileHandle
个人分类: iOS 开发笔记
上一篇swift3.0 复制文件
下一篇Android Toolbar使用
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭