iOS开发-文件管理

发布时间:2020-07-27 20:58:30 作者:Im刘亚芳
来源:网络 阅读:529

一、iOS中的沙盒机制

模拟器沙盒的位置

/User/userName/Library/Application Support/iPhone Simulator

当我们创建应用程序时,在每个沙盒中含有三个文件,分别是Document、Library和temp。


获取沙盒目录

NSString *homePath = NSHomeDirectory()

NSArray  *paths = NSSearchPathDorDirectoriesInDomains(NSDocumentDicrectory,, NSUserDomainMark, YES);                                                                           NSString *docPath = [paths lastObject];

NSArray *paths = NSSearchPathForDirectoriseInDomains(NSLibraryDirectory, NSUserDomainMask, YES);                                                                                   NSString *docPath = [paths lastObject];   

NSArray *paths = NSSearchPathForDirectoriseInDomains(NSCachesDirectory, NSUserDomainMask, YES);                                                                                   NSString *docPath = [paths lastObject];

NSString *temp = NSTemporaryDirectory( );


二、NSString类路径的处理方法

文件路径的处理

NSString *path = @"/Uesrs/apple/testfile.txt"

获得组成此路径的各个组成部分,结果:("/","User","apple","testfile.txt")

- (NSArray *)pathComponents;

提取路径的最后一个组成部分,结果:testfile.txt

- (NSString *)lastPathComponent;

删除路径的最后一个组成部分,结果:/Users/apple

- (NSString *)stringByDeletingLastPathCpmponent;

将path添加到先邮路径的末尾,结果:/Users/apple/testfile.txt/app.txt

- (NSString *)stringByAppendingPathConmponent:(NSString *)str;

去路径最后部分的扩展名,结果:text

- (NSString *)pathExtension;

删除路径最后部分的扩展名,结果:/Users/apple/testfile

- (NSString *)stringByDeletingPathExtension;

路径最后部分追加扩展名,结果:/User/apple/testfile.txt.jpg

- (NSString *)stringByAppendingPathExtension:(NSString *)str;


三、NSData

NSData的用法

1.NSString与NSData互相转换

NSData-> NSString                                                                                     NSString *aString = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding];

NSString->NSData                                                                                      NSString *aString = @"1234abcd";
NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding]; 

将data类型的数据,转成UTF8的数据

+(NSString *)dataToUTF8String:(NSData *)data
{
NSString *buf = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return [buf autorelease];
}

将string转换为指定编码 
+(NSString *)changeDataToEncodinString:(NSData *)data encodin:(NSStringEncoding )encodin{
    NSString *buf = [[[NSString alloc] initWithData:data encoding:encodin] autorelease];
    return buf;
}

2. NSData 与 UIImage
NSData->UIImage
UIImage *ap_w_picpath = [UIImage p_w_picpathWithData: p_w_picpathData];
 
//例:从本地文件沙盒中取图片并转换为NSData
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *name = [NSString stringWithFormat:@"ceshi.png"];
NSString *finalPath = [path stringByAppendingPathComponent:name];
NSData *p_w_picpathData = [NSData dataWithContentsOfFile: finalPath];
UIImage *ap_w_picpath = [UIImage p_w_picpathWithData: p_w_picpathData];

3.NSData与NSArray  NSDictionary

+(NSString *)getLocalFilePath:(NSString *) fileName
{
return [NSString stringWithFormat:@"%@/%@%@", NSHomeDirectory(),@“Documents”,fileName];
}

包括将NSData写进Documents目录
从Documents目录读取数据
在进行网络数据通信的时候,经常会遇到NSData类型的数据。在该数据是dictionary结构的情况下,系统没有提供现成的转换成NSDictionary的方法,为此可以通过Category对NSDictionary进行扩展,以支持从NSData到NSDictionary的转换。声明和实现如下:

 
+ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data {     
    CFPropertyListRef list = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (CFDataRef)data, kCFPropertyListImmutable, NULL);
    if(list == nil) return nil; 
    if ([(id)list isKindOfClass:[NSDictionary class]]) { 
         return [(NSDictionary *)list autorelease]; 
        } 
    else { 
         CFRelease(list); 
         return nil; 
        } 
}


四、文件管理常用方法

NSFileManager

创建一个文件并写入数据                                                                                    - (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;

从一个文件中读取数据                                                                                       - (NSData *)contentsAtPath:(NSString *)path;

scrPath路径上的文件移动到dstPath路径上,注意这里的路径是文件路径而不是目录          - (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **) error;

scrPath路径上的文件复制到dstPath路径上                                                            - (BOOL)copyItemAtPath:(NSString *)scrPath toPath:(NSString *)dstPath error:(NSError **) error;

比较两个文件的内容是否一样                                                                               - (BOOL)contentsEqualAtPath:(NSString *)path2 andPath:(NSString *)path3;

文件时候存在                                                                                                  - (BOOL)fileExistsAtPath:(NSString *)path;

移除文件                                                                                                        - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **) error;


创建文件管理

NSFileManager *fileManager = [NSFileManager defaultManager];                          NSString *path = [NSHomeDirectory( )  stringByAppendingPathComponent:@"holyBible.txt"];                                                                                                     NSString *text = @"abcdefg"; 

将字符串转成NSData类型                                                                                 NSData *data = [text dataUsingEncoding: NSUTF8StringEncoding]; 

写入文件                                                                                                       BOOL success = [fileManager createFileAtPath:path contents:data attributes:nil];


创建文件夹 

NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];     NSString *contect = @"abcdefg";                                                                     BOOL success = [fm createFileAtPath:filePath contents:[content dataUsingEncoding: NSUTF8StringEncoding] attributes:nil];


NSFileManager-读取内容                                                                                 NSData *fileData = [fileManager contentsAtPath:filePath];                                   NSString *content = [[NSString alloc] initWithData:fileData dataUsingEncoding: NSUTF8StringEncoding];

NSData-读取内容                                                                                          NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];     NSData *data = [NSData dataWithContentOfFile:filePath];

NSString-读取内容                                                                                         NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];     NSString *content = [[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

移动、复制文件                                                                                             

移动文件(重命名)                                                                                         NSString *toPath = [NSHomeDirectory( ) stringByAppendingPathComponent:@"hellogod/New Testament.txt"];                                                                              [fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];                                                   NSError *error;                                                                                             BOOL isSuccess = [fm moveItemAtPath:filePath toPath:toPath error:&error];

复制文件(重命名)                                                                                         NSString *copyPath = [NSHomeDirectory( ) stringByAppendingPathComponent:@"备份/Old Testament.txt"];                                                                                  [fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];                                                   BOOL success = [fm copyItemAtPath:toPath toPath:toPath error:nil];

删除文件、获取文件大小

判断文件是否存在和删除文件                                                                               if([fm fileExistsAtPath])                                                                                     {                                                                                                                    if ([fm removeItemAtPath:copyPath])                                                                {                                                                                                                   NSLog(@"remove success");                                                                            }                                                                                                                  }

获取文件大小                                                                                                  NSFileManager *fileManager = [NSFileManager defaultManager];                         获得文件的属性字典                                                                                         NSDictionary *attrDic = [fileManager attributesOfItemAtpath:sourcePath error:nil];  NSNumber *fileSize = [attrDic objectForKey:NSFileSize];   

获取目录文件信息                                                                                            NSFileManager *fileManager = [NSFileManager defaultManager];                         NSString *enuPath = [NSHomeDirectoty( ) stringByAppendingPathComponent:@"Test"];                                                                                                           NSDictionaryEnumerator *dirEnum = [fileManager enumeratorAtPath:enuPath];     NSString *path = nil;                                                                                      while ((path = [dirEnum nextObject]} != nil)                                                        {                                                                                                                  NSLog(@"%@",path);                                                                                        }


推荐阅读:
  1. 文件管理,实现高效办公
  2. 目录和文件管理(二)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

应用程序 iphone 文件管理

上一篇:MongoDB的查询

下一篇:IBM Power6、7配件FC号、描述翻译(unix360.part02)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》