NSFileManager文件 *** 作的十个小功能

NSFileManager文件 *** 作的十个小功能,第1张

概述NSFileManager文件 *** 作的十个小功能

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

- (Nsstring *)getdocumentsPath{    //获取documents路径    NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES);    Nsstring *path = [paths objectAtIndex:0];    NSLog(@"path:%@",path);    return path;} 
创建文件夹
-(voID)createDirectory{    Nsstring *documentsPath =[self getdocumentsPath];    NSfileManager *fileManager = [NSfileManager defaultManager];    Nsstring *iOSDirectory = [documentsPath stringByAppendingPathComponent:@"iOS"];    BOol isSuccess = [fileManager createDirectoryAtPath:iOSDirectory withIntermediateDirectorIEs:YES attributes:nil error:nil];    if (isSuccess) {        NSLog(@"success");    } else {        NSLog(@"fail");    }} 
创建文件
-(voID)createfile{    Nsstring *documentsPath =[self getdocumentsPath];    NSfileManager *fileManager = [NSfileManager defaultManager];    Nsstring *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];    BOol isSuccess = [fileManager createfileAtPath:iOSPath contents:nil attributes:nil];    if (isSuccess) {        NSLog(@"success");    } else {        NSLog(@"fail");    }} 
写文件
-(voID)writefile{    Nsstring *documentsPath =[self getdocumentsPath];    Nsstring *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];    Nsstring *content = @"我要写数据啦";    BOol isSuccess = [content writetofile:iOSPath atomically:YES enCoding:NSUTF8StringEnCoding error:nil];    if (isSuccess) {        NSLog(@"write success");    } else {        NSLog(@"write fail");    }} 
读取文件内容
-(voID)readfileContent{    Nsstring *documentsPath =[self getdocumentsPath];    Nsstring *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];    Nsstring *content = [Nsstring stringWithContentsOffile:iOSPath enCoding:NSUTF8StringEnCoding error:nil];    NSLog(@"read success: %@",content);} 
判断文件是否存在
- (BOol)isSxistAtPath:(Nsstring *)filePath{    NSfileManager *fileManager = [NSfileManager defaultManager];    BOol isExist = [fileManager fileExistsAtPath:filePath];    return isExist;} 
计算文件大小
- (unsigned long long)fileSizeAtPath:(Nsstring *)filePath{    NSfileManager *fileManager = [NSfileManager defaultManager];    BOol isExist = [fileManager fileExistsAtPath:filePath];    if (isExist){        unsigned long long fileSize = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];        return fileSize;    } else {        NSLog(@"file is not exist");        return 0;    }} 
计算整个文件夹中所有文件大小
- (unsigned long long)folderSizeAtPath:(Nsstring*)folderPath{    NSfileManager *fileManager = [NSfileManager defaultManager];    BOol isExist = [fileManager fileExistsAtPath:folderPath];    if (isExist){        NSEnumerator *childfileEnumerator = [[fileManager subpathsAtPath:folderPath] objectEnumerator];        unsigned long long folderSize = 0;        Nsstring *filename = @"";        while ((filename = [childfileEnumerator nextObject]) != nil){            Nsstring* fileabsolutePath = [folderPath stringByAppendingPathComponent:filename];            folderSize += [self fileSizeAtPath:fileabsolutePath];        }        return folderSize / (1024.0 * 1024.0);    } else {        NSLog(@"file is not exist");        return 0;    }} 
删除文件
-(voID)deletefile{    Nsstring *documentsPath =[self getdocumentsPath];    NSfileManager *fileManager = [NSfileManager defaultManager];    Nsstring *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];    BOol isSuccess = [fileManager removeItemAtPath:iOSPath error:nil];    if (isSuccess) {        NSLog(@"delete success");    }else{        NSLog(@"delete fail");    }} 
移动文件
- (voID)movefilename{    Nsstring *documentsPath =[self getdocumentsPath];    NSfileManager *fileManager = [NSfileManager defaultManager];    Nsstring *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];    Nsstring *movetoPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];    BOol isSuccess = [fileManager moveItemAtPath:filePath topath:movetoPath error:nil];    if (isSuccess) {        NSLog(@"rename success");    }else{        NSLog(@"rename fail");    }} 
重命名
- (voID)renamefilename{    //通过移动该文件对文件重命名    Nsstring *documentsPath =[self getdocumentsPath];    NSfileManager *fileManager = [NSfileManager defaultManager];    Nsstring *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];    Nsstring *movetoPath = [documentsPath stringByAppendingPathComponent:@"rename.txt"];    BOol isSuccess = [fileManager moveItemAtPath:filePath topath:movetoPath error:nil];    if (isSuccess) {        NSLog(@"rename success");    }else{        NSLog(@"rename fail");    }} 

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的NSFileManager文件 *** 作的十个小功能全部内容,希望文章能够帮你解决NSFileManager文件 *** 作的十个小功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1106126.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存