如何在我的应用程序本地文档目录(UTF-8)上将NSString保存为.txt文件?如何将NSString作为.txt文件保存在我的应用程序本地文档目录中?
回答
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSError *error;
BOOL succeed = [myString writeToFile:[documentsDirectory stringByAppendingPathComponent:@"myfile.txt"]
atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!succeed){
// Handle error here
}
你可以使用NSUserDefaults的
节能:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];
阅读:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = [prefs stringForKey:@"keyToLookupString"];
哥们,.txt文件 – RexOnRoids 2010-02-02 13:43:49
同意你可以,这是一个有效的替代。不是他要求的,但如果你不知道使用这些首选项有多容易,你不会知道使用它们。 +1 – 2011-01-18 16:14:30
不要忘记在将某些内容保存到NSUserDefaults后调用'[prefs synchronize]'! – 2014-10-26 10:37:27
事情是这样的:
NSString *homeDirectory;
homeDirectory = NSHomeDirectory(); // Get app's home directory - you could check for a folder here too.
BOOL isWriteable = [[NSFileManager defaultManager] isWritableFileAtPath: homeDirectory]; //Check file path is writealbe
// You can now add a file name to your path and the create the initial empty file
[[NSFileManager defaultManager] createFileAtPath:newFilePath contents:nil attributes:nil];
// Then as a you have an NSString you could simple use the writeFile: method
NSString *yourStringOfData;
[yourStringOfData writeToFile: newFilePath atomically: YES];
他是如何将NSString保存到Documents文件夹中的。保存其他类型的数据也可以通过这种方式实现。
- (void)saveStringToDocuments:(NSString *)stringToSave {
NSString *documentsFolder = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *fileName = [NSString stringWithString:@"savedString.txt"];
NSString *path = [documentsFolder stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] createFileAtPath:path contents:[stringToSave dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
}
我正在使用此方法将一些base64编码图像数据保存到磁盘。在我的电脑上打开文本文件时,由于一些换行符和返回被自动添加,我一直无法读取数据。
下面的代码修复此问题:
myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
myString = [myString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
// write string to disk
- 1. 如何将文件放在我的应用程序的文档目录中?
- 2. 如何将iphone应用程序中的数据保存到文本文件并保存在文档中
- 3. 如何获取文本文件的内容(本地存储在文档目录中)到NSString中?
- 4. 用相对路径将文件保存在文档目录中?
- 5. 如何在文档目录中使用MPMediaPickerController和本地文件?
- 6. 如何在文档目录中保护我的应用程序数据
- 7. 将文件保存在iOS应用程序目录
- 8. 如何将android应用资产文件夹中的文本文件保存为android手机的本地目录作为文本或pdf文件?
- 9. 如何将Ruby中的put文本保存到txt文件中?
- 10. 如何将git存储保存为文件或文件目录?
- 11. 如何将文件保存到本地目录
- 12. 如何将文件夹添加到iOS应用程序文档目录中?
- 13. 将iOS应用程序音频文件应用程序存储在xcassets或文档目录中?
- 14. 从应用程序保存应用程序目录中的文件
- 15. 如何将.txt文件保存在系统被访问的PC目录中(Python)
- 16. iOS保存来自本地文档目录的远程目录中的所有文件
- 17. 如何使用指定的NSString编码在Cocoa中保存文本文档?
- 18. 我可以在iOS的应用程序文档目录中写入文件吗?
- 19. 如何附加本地存储在应用程序目录中的文本文件
- 20. 将.txt文件从ftp保存到文档文件夹iphone
- 21. 我如何保存到我的iPhone应用程序的文本文件
- 22. 我想将解析的HTML文件保存为TXT文件
- 23. 如何在应用程序中放置资源文档目录
- 24. 将NSString保存到文件
- 25. 将NSMutableDictionary保存到文档目录
- 26. PDFkit js如何将文档保存到文件(Win 8应用程序)
- 27. 如何在C++中保存.txt文件?
- 28. 将文件保存在应用程序文件夹中
- 29. iOS将图像保存到文档目录放慢应用程序
- 30. 如何在Delphi中将字符串保存为.txt文件?
不要为'error:'参数传递'nil'。首先,它是错误的类型('nil'在概念上是'id',而'error:'参数是'NSError **',基本上是'id *');正确类型的常量是'NULL'。更重要的是,当这个声明失败时,你不想知道为什么吗? – 2010-06-17 06:29:38
进度,但当消息成功时,您也不应该认为'error'是'nil'。测试消息是否返回“YES”或“NO”,并在返回“NO”时仅检查错误对象。引用:http://developer.apple.com/mac/library/documentation/cocoa/conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html#//apple_ref/doc/uid/TP40001806-CH204-SW1(第一个“重要的”侧栏 - 诚然,这是有点斜了) – 2010-06-17 08:25:21
感谢您的意见 – Vladimir 2010-06-17 08:39:37