iOS文件操作的十个小功能
2016-02-18 16:13
561 查看
NSFileManager是一个单列类,也是一个文件管理器。可以通过NSFileManager创建文件夹、创建文件、写文件、读文件内容等等基本功能。
下面将介绍NSFileManager文件操作的十个小功能。我们在Documents里面进行举例,首先是获取Documents的路径。这个在iOS开发之沙盒机制(SandBox)已经详细讲解过了。获取Documents路径方法如下:
-(NSString*)getDocumentsPath
{
//获取Documents路径
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*path=[pathsobjectAtIndex:0];
NSLog(@"path:%@",path);
returnpath;
}
NSString*documentsPath=[selfgetDocumentsPath];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
NSString*iOSDirectory=[documentsPathstringByAppendingPathComponent:@"iOS"];
BOOLisSuccess=[fileManagercreateDirectoryAtPath:iOSDirectorywithIntermediateDirectories:YESattributes:nilerror:nil];
if(isSuccess){
NSLog(@"success");
}else{
NSLog(@"fail");
}
}
NSString*documentsPath=[selfgetDocumentsPath];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
NSString*iOSPath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
BOOLisSuccess=[fileManagercreateFileAtPath:iOSPathcontents:nilattributes:nil];
if(isSuccess){
NSLog(@"success");
}else{
NSLog(@"fail");
}
}
NSString*documentsPath=[selfgetDocumentsPath];
NSString*iOSPath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
NSString*content=@"我要写数据啦";
BOOLisSuccess=[contentwriteToFile:iOSPathatomically:YESencoding:NSUTF8StringEncodingerror:nil];
if(isSuccess){
NSLog(@"writesuccess");
}else{
NSLog(@"writefail");
}
}
NSString*documentsPath=[selfgetDocumentsPath];
NSString*iOSPath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
NSString*content=[NSStringstringWithContentsOfFile:iOSPathencoding:NSUTF8StringEncodingerror:nil];
NSLog(@"readsuccess:%@",content);
}
NSFileManager*fileManager=[NSFileManagerdefaultManager];
BOOLisExist=[fileManagerfileExistsAtPath:filePath];
returnisExist;
}
NSFileManager*fileManager=[NSFileManagerdefaultManager];
BOOLisExist=[fileManagerfileExistsAtPath:filePath];
if(isExist){
unsignedlonglongfileSize=[[fileManagerattributesOfItemAtPath:filePatherror:nil]fileSize];
returnfileSize;
}else{
NSLog(@"fileisnotexist");
return0;
}
}
NSFileManager*fileManager=[NSFileManagerdefaultManager];
BOOLisExist=[fileManagerfileExistsAtPath:folderPath];
if(isExist){
NSEnumerator*childFileEnumerator=[[fileManagersubpathsAtPath:folderPath]objectEnumerator];
unsignedlonglongfolderSize=0;
NSString*fileName=@"";
while((fileName=[childFileEnumeratornextObject])!=nil){
NSString*fileAbsolutePath=[folderPathstringByAppendingPathComponent:fileName];
folderSize+=[selffileSizeAtPath:fileAbsolutePath];
}
returnfolderSize/(1024.0*1024.0);
}else{
NSLog(@"fileisnotexist");
return0;
}
}
NSString*documentsPath=[selfgetDocumentsPath];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
NSString*iOSPath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
BOOLisSuccess=[fileManagerremoveItemAtPath:iOSPatherror:nil];
if(isSuccess){
NSLog(@"deletesuccess");
}else{
NSLog(@"deletefail");
}
}
{
NSString*documentsPath=[selfgetDocumentsPath];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
NSString*filePath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
NSString*moveToPath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
BOOLisSuccess=[fileManagermoveItemAtPath:filePathtoPath:moveToPatherror:nil];
if(isSuccess){
NSLog(@"renamesuccess");
}else{
NSLog(@"renamefail");
}
}
{
//通过移动该文件对文件重命名
NSString*documentsPath=[selfgetDocumentsPath];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
NSString*filePath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
NSString*moveToPath=[documentsPathstringByAppendingPathComponent:@"rename.txt"];
BOOLisSuccess=[fileManagermoveItemAtPath:filePathtoPath:moveToPatherror:nil];
if(isSuccess){
NSLog(@"renamesuccess");
}else{
NSLog(@"renamefail");
}
}
下面将介绍NSFileManager文件操作的十个小功能。我们在Documents里面进行举例,首先是获取Documents的路径。这个在iOS开发之沙盒机制(SandBox)已经详细讲解过了。获取Documents路径方法如下:
-(NSString*)getDocumentsPath
{
//获取Documents路径
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*path=[pathsobjectAtIndex:0];
NSLog(@"path:%@",path);
returnpath;
}
创建文件夹
-(void)createDirectory{NSString*documentsPath=[selfgetDocumentsPath];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
NSString*iOSDirectory=[documentsPathstringByAppendingPathComponent:@"iOS"];
BOOLisSuccess=[fileManagercreateDirectoryAtPath:iOSDirectorywithIntermediateDirectories:YESattributes:nilerror:nil];
if(isSuccess){
NSLog(@"success");
}else{
NSLog(@"fail");
}
}
创建文件
-(void)createFile{NSString*documentsPath=[selfgetDocumentsPath];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
NSString*iOSPath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
BOOLisSuccess=[fileManagercreateFileAtPath:iOSPathcontents:nilattributes:nil];
if(isSuccess){
NSLog(@"success");
}else{
NSLog(@"fail");
}
}
写文件
-(void)writeFile{NSString*documentsPath=[selfgetDocumentsPath];
NSString*iOSPath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
NSString*content=@"我要写数据啦";
BOOLisSuccess=[contentwriteToFile:iOSPathatomically:YESencoding:NSUTF8StringEncodingerror:nil];
if(isSuccess){
NSLog(@"writesuccess");
}else{
NSLog(@"writefail");
}
}
读取文件内容
-(void)readFileContent{NSString*documentsPath=[selfgetDocumentsPath];
NSString*iOSPath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
NSString*content=[NSStringstringWithContentsOfFile:iOSPathencoding:NSUTF8StringEncodingerror:nil];
NSLog(@"readsuccess:%@",content);
}
判断文件是否存在
-(BOOL)isSxistAtPath:(NSString*)filePath{NSFileManager*fileManager=[NSFileManagerdefaultManager];
BOOLisExist=[fileManagerfileExistsAtPath:filePath];
returnisExist;
}
计算文件大小
-(unsignedlonglong)fileSizeAtPath:(NSString*)filePath{NSFileManager*fileManager=[NSFileManagerdefaultManager];
BOOLisExist=[fileManagerfileExistsAtPath:filePath];
if(isExist){
unsignedlonglongfileSize=[[fileManagerattributesOfItemAtPath:filePatherror:nil]fileSize];
returnfileSize;
}else{
NSLog(@"fileisnotexist");
return0;
}
}
计算整个文件夹中所有文件大小
-(unsignedlonglong)folderSizeAtPath:(NSString*)folderPath{NSFileManager*fileManager=[NSFileManagerdefaultManager];
BOOLisExist=[fileManagerfileExistsAtPath:folderPath];
if(isExist){
NSEnumerator*childFileEnumerator=[[fileManagersubpathsAtPath:folderPath]objectEnumerator];
unsignedlonglongfolderSize=0;
NSString*fileName=@"";
while((fileName=[childFileEnumeratornextObject])!=nil){
NSString*fileAbsolutePath=[folderPathstringByAppendingPathComponent:fileName];
folderSize+=[selffileSizeAtPath:fileAbsolutePath];
}
returnfolderSize/(1024.0*1024.0);
}else{
NSLog(@"fileisnotexist");
return0;
}
}
删除文件
-(void)deleteFile{NSString*documentsPath=[selfgetDocumentsPath];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
NSString*iOSPath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
BOOLisSuccess=[fileManagerremoveItemAtPath:iOSPatherror:nil];
if(isSuccess){
NSLog(@"deletesuccess");
}else{
NSLog(@"deletefail");
}
}
移动文件
-(void)moveFileName{
NSString*documentsPath=[selfgetDocumentsPath];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
NSString*filePath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
NSString*moveToPath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
BOOLisSuccess=[fileManagermoveItemAtPath:filePathtoPath:moveToPatherror:nil];
if(isSuccess){
NSLog(@"renamesuccess");
}else{
NSLog(@"renamefail");
}
}
重命名
-(void)renameFileName{
//通过移动该文件对文件重命名
NSString*documentsPath=[selfgetDocumentsPath];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
NSString*filePath=[documentsPathstringByAppendingPathComponent:@"iOS.txt"];
NSString*moveToPath=[documentsPathstringByAppendingPathComponent:@"rename.txt"];
BOOLisSuccess=[fileManagermoveItemAtPath:filePathtoPath:moveToPatherror:nil];
if(isSuccess){
NSLog(@"renamesuccess");
}else{
NSLog(@"renamefail");
}
}
相关文章推荐
- iOS-定时器
- 用CocoaPods做iOS程序的依赖管理
- GCD IOS程序等一会在执行,延时执行
- FFmpeg完美编译iOS版本
- GCD iOS开发
- 超全!整理常用的iOS第三方资源
- Openstack: MP-BIOS bug: 8254 timer not connected to IO-APIC
- Openstack: MP-BIOS bug: 8254 timer not connected to IO-APIC
- iOS的几种传值方法
- ios应用程序的应用构架及规则
- ios5 模拟器上没 home键,怎么返回的
- iOS内存管理
- iOS9 - SFSafariViewController提供完整的Web浏览体验
- IOS学习路线图
- ios的AutoresizingMask
- iOS 集成友盟分享
- iOS tableViewCell侧滑改变收藏状态
- iOS关于日期格式化字符串的问题
- iOS 计算label 字符串的高度
- 长按手势添加到collectionView