如何创建用于写入的文本文件 [英] How to create text file for writing

查看:74
本文介绍了如何创建用于写入的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文件中写入文本时遇到问题.到目前为止,我已经完成了以下工作:我有一个字符串,其中要存储文本,文件名也作为字符串.

I've a problem to write text in a file. What I've done so far is the following: I have a string with the text to store and the file name also as a string.

let someText = "abcd"
let fileName = "file:///xxx"

当然"xxx"是文档目录下的.txt文件,因此应该可以编写.
然后我发现可以使用字符串的write方法.但是对于此调用,我需要将文件名作为url,所以我需要这段代码:

Of course "xxx" is a .txt file under the document directory so it should be possible to write.
Then I found out that I can use the write method o the string. But for this call I need the file name as url so I have this piece of code:

let fileUrl = URL(string: fileName)
do {
    try someText.write(to: fileUrl, atomically: false, encoding: String.Encoding.utf8)
}
catch { }

如果我启动我的应用程序,则会收到错误消息文件xxx不存在".好的,这是正确的,因为未创建文件.我以为write方法会自动执行此操作,但事实并非如此.
这就是我不知道如何解决此问题的要点!

If I start my app then I will get an error "The file xxx does not exist". Ok that's correct because the file is not created. I thought that the write method does it automatically but it seems not so.
And that's the point I don't know how to solve this issue!

我正在使用Xcode 8 + Swift 3.

I'm using Xcode 8 + Swift 3.

+++编辑+++

我试图解释我到底在寻找什么.
假设我有两个任务:第一个任务构建文件名并将其存储在数据库中.这就是为什么我使用字符串的原因:

I try to explain what I'm exactly looking for.
Let's say I've two tasks: The first task builds file names and stores it in a database. That's why I work with strings:

var fileName = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).path
if (!fileName.hasSuffix("/")) {
    fileName += "/"
}
fileName += "file.txt"

如您所见,该文件目前尚未创建,因为在此任务中我只需要名称.

As you can see the file is not created at this moment because I only need the name in this task.

确定,然后执行第二项任务.它必须从数据库中选择一个特定的文件名,并在其后附加file://方案:

Ok and then the second task. It has to select a specific file name from the database and append it with the file:// scheme:

let fileName = "file://" + fileNameFromDatabase

然后使用上面的代码编写文本.
很显然,此任务中不存在该文件,因为我只有名字.这就是为什么我认为write方法的错误消息是正确的.
我现在正在寻找一种可以一步创建文件/写入文本的可能性.

Then the text is written with the code above.
It's clear that the file not exists in this task because I only have the name. That's why I think that the error message of the write method is correct.
What I'm now looking for is a possibility to create the file/write the text in one step.

推荐答案

Swift 3示例:

Swift 3 example:

func write(text: String, to fileNamed: String, folder: String = "SavedFiles") {
    guard let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { return }
    guard let writePath = NSURL(fileURLWithPath: path).appendingPathComponent(folder) else { return }
    try? FileManager.default.createDirectory(atPath: writePath.path, withIntermediateDirectories: true)
    let file = writePath.appendingPathComponent(fileNamed + ".txt")
    try? text.write(to: file, atomically: false, encoding: String.Encoding.utf8)
}

这篇关于如何创建用于写入的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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