10 small functions for Nsfilemanager file operations

Source: Internet
Author: User

Nsfilemanager is a single-column class and also a file manager. You can create folders, create files, write files, read file contents, and so on through Nsfilemanager.

The following is a description of the 10 small features of the Nsfilemanager file operation. For example, in documents, we first get the path to documents. This sandbox mechanism (sandbox) developed in iOS has been explained in detail. Here's how to get the documents path:

- (NSString *)getDocumentsPath{    //获取Documents路径    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *path = [paths objectAtIndex:0];    NSLog(@"path:%@", path);    return path;}
Create a folder
-(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");    }}
Create a file
-(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");    }}
Write a file
-(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");    }}
Read File contents
-(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);}
Determine if a file exists
- (BOOL)isSxistAtPath:(NSString *)filePath{    NSFileManager *fileManager = [NSFileManager defaultManager];    BOOL isExist = [fileManager fileExistsAtPath:filePath];    return isExist;}
Calculate File Size
- (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;    }}
Calculate all file sizes in an entire folder
-(unsigned long Long) Foldersizeatpath: (nsstring*) folderpath{nsfilemanager *filemanager = [Nsfilemanager defaultmanag    ER];    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 Stringby            Appendingpathcomponent:filename];        Foldersize + = [self filesizeatpath:fileabsolutepath];    } return foldersize/(1024.0 * 1024.0);        } else {NSLog (@ "file is not exist");    return 0; }}
deleting files
-(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");    }}
Moving files
- (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");    }}
Renaming
- (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"); }}

The above file operation code is Iosstrongdemo, can be pulled down directly from git to test. There are a lot of ways to use Nsfilemanager.

?

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

10 small functions for Nsfilemanager file operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.