URL,data(网络下载,url编码,文件读写),NSFileManager(文件管理者,系统单例)

来源:互联网 发布:映射网络驱动器 编辑:程序博客网 时间:2024/06/06 20:45

//获取工程的根目录

NSString *rootPath = NSHomeDirectory();

//获取Documents、library、tmp目录的方法:

// 获取Documents的目录的方法:

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];NSLog(@"documentsPath = %@",documentsPath);

// 获取Library目录的方法:

NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];NSLog(@"libraryPath = %@",libraryPath);

// 获取tmp目录的方法:(比较简单)

NSTemporaryDirectory();NSString *tmpPath = NSTemporaryDirectory();NSLog(@"tmpPath = %@",tmpPath);

//创建一个文件管理者(系统单例对象)
这个对象可以进行文件夹的操作(增、删、判断文件夹是否存在 ,还可以进行文件的写入)

NSFileManager *fileManager = [NSFileManager defaultManager];
#pragma mark - NSFileManager 文件管理者,系统单例//(增、删、判断文件夹是否存在 ,还可以进行文件的写入)-(void)fileManager{    NSFileManager *fileManager = [NSFileManager defaultManager];//    增文件夹:   BOOL isSuccess = [fileManager createDirectoryAtPath:@"想要创建的目标路径" withIntermediateDirectories:YES attributes:nil error:nil];//    减文件夹:    isSuccess =[fileManager removeItemAtPath:@"想要删除的目标路径" error:nil];//    判断文件夹是否存在:    isSuccess = [fileManager isExecutableFileAtPath:@"想要判断是否存在的文件夹的目录"];//    文件的写入,    data也有类似的功能//    contents:  这个参数就是填 要写入文件的data二进制数据了    /*    isSuccess = [fileManager createFileAtPath:@"想要写入的文件的路径(包括后面的名字)" contents:(nullable NSData *) attributes:nil];    */}     

字符串、数组、字典写入本地文件,获取文件夹下文件的目录数组,计算文件夹的大小,删除文件

//    将字符串写入到本地    NSString * targetString = @"HELLO GUYS";    BOOL flag = [targetString writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"a.txt"] atomically:YES encoding:NSUTF8StringEncoding error:nil];    if (flag)    {        NSLog(@"写入 OK ");    } else {        NSLog(@"写入 不OK");    }//    数组写入本地    NSArray *array = @[@"六",@"王",@"流",@"刘"];    flag = [array writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"array.txt"] atomically:YES];    if (flag)    {         NSLog(@"写入数组成功 ");    }else    {         NSLog(@"写入数组失败");    }//    将字典写入本地    NSDictionary *dic = @{@"name":@"Rick",@"age":@25,@"address":@"GZ"};    flag = [dic writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"dic.txt"] atomically:YES];    if (flag)    {        NSLog(@"写入字典成功 ");    }else    {        NSLog(@"写入字典失败");    }//    计算文件的大小//    获得将要计算的文件夹    NSFileManager *fileManager = [NSFileManager defaultManager];//    获得imgs目录下文件名组成的数组    NSArray *imgsFileArray = [fileManager subpathsAtPath:imgsTmpPath];    NSLog(@"imgsFileArray = %@",imgsFileArray);    CGFloat count = 0;    for (NSString *ele in imgsFileArray)    {        NSData *data = [NSData dataWithContentsOfFile:[imgsTmpPath stringByAppendingPathComponent:ele]];        count += data.length;    }    count = count/1024/1024;    NSLog(@"缓存文件的大小为: %.2fM",count);//    删除文件    for (NSString *ele in imgsFileArray)    {        BOOL isSuccess = [fileManager removeItemAtPath:[imgsTmpPath stringByAppendingPathComponent:ele] error:nil];        if (isSuccess)        {            NSLog(@"删除成功");        } else {            NSLog(@"删除失败");        }    }

声明:

1.以上内容属于本人整理的笔记,如有错误的地方希望能告诉我,大家共同进步。2.以上内容有些段落或语句可能是本人从其他地方Copy而来,如有侵权,请及时告诉我。
0 0
原创粉丝点击