- (void)viewDidLoad {
[super viewDidLoad];
/**
* @brief 获取文件路径
*
*/
NSString * filePath = [self getfilePath];
NSLog(@"%@",filePath);
/**
* @brief 字符串的读写
*/
[self stringWR];
/**
* @brief 数组的读写
*/
[self ArrayWR];
/**
* @brief NSData的读写
*/
[self DataWR];
}
- (NSString *)getfilePath
{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths firstObject];
return [documentsDirectory stringByAppendingPathComponent:@"myDocment.txt"];
}
- (void)stringWR
{
NSString * str = @"我的测试数据";
if ([str writeToFile:[self getfilePath] atomically:YES encoding:NSUTF8StringEncoding error:nil]) {
NSLog(@"成功");
}else{
NSLog(@"失败");
}
NSString * text = [NSString stringWithContentsOfFile:[self getfilePath] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",text);
}
- (void)ArrayWR
{
NSArray * dataArr = @[@"adc",@"acsdsda"];
if ([dataArr writeToFile:[self getfilePath] atomically:YES]) {
NSLog(@"成功");
}
NSArray * arr = [NSArray arrayWithContentsOfFile:[self getfilePath]];
NSLog(@"%@",arr[0]);
}
- (void)DataWR
{
NSData * data = [@"sdadsasd" dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
if ([data writeToFile:[self getfilePath] atomically:YES]) {
NSLog(@"成功");
}
NSData * data1 = [NSData dataWithContentsOfFile:[self getfilePath]];
NSString * newStr = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
NSLog(@"%@",newStr);
}