如何在我的应用程序本地文档目录中将NSString保存为.txt文件?

时间:2022-06-08 07:23:55

How do I save an NSString as a .txt file on my apps local documents directory (UTF-8)?

如何在我的应用程序本地文档目录(UTF-8)上将NSString保存为.txt文件?

5 个解决方案

#1


42  

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
}

#2


3  

Something like this:

像这样的东西:

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];

#3


1  

He is how to save NSString into Documents folder. Saving other types of data can be also realized that way.

他是如何将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];
}

#4


0  

you could use NSUserDefaults

你可以使用NSUserDefaults

Saving:

保存:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

Reading:

读:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = [prefs stringForKey:@"keyToLookupString"];

#5


0  

I was using this method to save some base64 encoded image data to the disk. When opening the text file on my computer I kept having trouble reading the data because of some line breaks and returns being added automatically.

我正在使用此方法将一些base64编码的图像数据保存到磁盘。在我的计算机上打开文本文件时,由于一些换行符和自动添加的返回值,我一直无法读取数据。

The following code fixes this issue:

以下代码修复了此问题:

myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
myString = [myString stringByReplacingOccurrencesOfString:@"\r" withString:@""];

// write string to disk

#1


42  

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
}

#2


3  

Something like this:

像这样的东西:

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];

#3


1  

He is how to save NSString into Documents folder. Saving other types of data can be also realized that way.

他是如何将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];
}

#4


0  

you could use NSUserDefaults

你可以使用NSUserDefaults

Saving:

保存:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

Reading:

读:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = [prefs stringForKey:@"keyToLookupString"];

#5


0  

I was using this method to save some base64 encoded image data to the disk. When opening the text file on my computer I kept having trouble reading the data because of some line breaks and returns being added automatically.

我正在使用此方法将一些base64编码的图像数据保存到磁盘。在我的计算机上打开文本文件时,由于一些换行符和自动添加的返回值,我一直无法读取数据。

The following code fixes this issue:

以下代码修复了此问题:

myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
myString = [myString stringByReplacingOccurrencesOfString:@"\r" withString:@""];

// write string to disk