我试图用:
来保存一个简单的文件Scanner sc = new Scanner(System.in);
Map<String, String> morsecode = new HashMap<>();
morsecode.put("a", ",-");
morsecode.put("b", "-...");
// will add other letters later
System.out.println("please enter an english sentence:");
String line = sc.nextLine();
for (char ch : line.toLowerCase().toCharArray()) {
System.out.print(morsecode.get(Character.toString(ch)) + " ");
}
System.out.println();
但无论我选择哪个目录,都会将其保存为
/ Users / jaredearl / Library / Developer / CoreSimulator / Devices / 6D477D99-7741-472D-8D16-4AE6771AF92E / data / Containers / Data / Appli ... file.txt
那个标签会在重新启动时发生变化,当我使用类似的东西时会发生变化:
if let documents = directories.first {
if let urlDocuments = URL(string: documents) {
let urlText = urlDocuments.appendingPathComponent("file.txt")
print(urlText)
do {
try text.write(to: urlText, atomically: false, encoding: .utf8)
print(text)
}
catch {}
true)
}
}
然后当我尝试读取我得到的文件时:NSURLConnection完成错误 - 代码-1002
如何让它在重新启动后持续存在?
答案 0 :(得分:1)
在swift 3.0中
您可以使用相同的功能在文件中读/写
func storeSyncLog(txtStor:String) {
let fileName = "a.txt"
let dir = try? FileManager.default.url(for: .documentDirectory,in: .userDomainMask, appropriateFor: nil, create: true)
//If the directory was found, we write a file to it and read it back
if let fileURL = dir?.appendingPathComponent(fileName).appendingPathExtension("txt") {
var inString = ""
do {
inString = try String(contentsOf: fileURL)
} catch {
print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
}
//Write something in file
let outString = inString + "Date:\(Date()) yd : \(txtStor)\n\n"
do {
try outString.write(to: fileURL, atomically: true, encoding: .utf8)
} catch {
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}
}
}
希望得到帮助