| 本帖最后由 竹林风 于 2018-12-24 14:54 编辑 
 
   文章导航   
 
 一.iOS中的沙盒机制
 
 iOS应用程序只能对自己创建的文件系统读取文件,这个独立、封闭、安全的空间,叫做沙盒。它一般存放着程序包文件(可执行文件)、图片、音频、视频、plist文件、sqlite数据库以及其他文件。 每个应用程序都有自己的独立的存储空间(沙盒) 一般来说应用程序之间是不可以互相访问 
 模拟器沙盒的位置: /User/userName/Library/Application Support/iPhone Simulator 
 当我们创建应用程序时,在每个沙盒中含有三个文件,分别是Document、Library和temp。 
 获取沙盒目录Document:一般需要持久的数据都放在此目录中,可以在当中添加子文件夹,iTunes备份和恢复的时候,会包括此目录。Library:设置程序的默认设置和其他状态信息temp:创建临时文件的目录,当iOS设备重启时,文件会被自动清除
 
 
 
 获取程序的根目录(home)目录
 
 [Objective-C] 纯文本查看 复制代码 NSString *homePath = NSHomeDirectory()获取Document目录
 
 [Objective-C] 纯文本查看 复制代码 NSArray  *paths = NSSearchPathDorDirectoriesInDomains(NSDocumentDicrectory,, NSUserDomainMark, YES);    NSString *docPath = [paths lastObject];
 获取Library目录 [Objective-C] 纯文本查看 复制代码 NSArray *paths = NSSearchPathForDirectoriseInDomains(NSLibraryDirectory, NSUserDomainMask, YES);                                                                                           NSString *docPath = [paths lastObject];获取Library中的Cache [Objective-C] 纯文本查看 复制代码 NSArray *paths = NSSearchPathForDirectoriseInDomains(NSCachesDirectory, NSUserDomainMask, YES);                                                                                   NSString *docPath = [paths lastObject];获取temp路径 [Objective-C] 纯文本查看 复制代码 NSString *temp = NSTemporaryDirectory( );二、NSString类路径的处理方法
 
 文件路径的处理
 
 [Objective-C] 纯文本查看 复制代码 
NSString *path = @"/Uesrs/apple/testfile.txt"
 
 常用方法如下: 
 获得组成此路径的各个组成部分,结果:("/","User","apple","testfile.txt") [Objective-C] 纯文本查看 复制代码 
- (NSArray *)pathComponents;
 提取路径的最后一个组成部分,结果:testfile.txt [Objective-C] 纯文本查看 复制代码 - (NSString *)lastPathComponent;
 删除路径的最后一个组成部分,结果:/Users/apple [Objective-C] 纯文本查看 复制代码 - (NSString *)stringByDeletingLastPathCpmponent;
 将path添加到先邮路径的末尾,结果:/Users/apple/testfile.txt/app.txt [Objective-C] 纯文本查看 复制代码 - (NSString *)stringByAppendingPathConmponent:(NSString *)str;
 取路径最后部分的扩展名,结果:txt [Objective-C] 纯文本查看 复制代码 - (NSString *)pathExtension;
 删除路径最后部分的扩展名,结果:/Users/apple/testfile [Objective-C] 纯文本查看 复制代码 - (NSString *)stringByDeletingPathExtension;
 路径最后部分追加扩展名,结果:/User/apple/testfile.txt.jpg [Objective-C] 纯文本查看 复制代码 - (NSString *)stringByAppendingPathExtension:(NSString *)str;
三、文件的操作
 
 1.创建文件夹
 
 [Objective-C] 纯文本查看 复制代码 //创建文件夹
-(void *)createDir{
    NSString *documentsPath =[self dirDoc];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    // 创建目录
    BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    if (res) {
        NSLog(@"文件夹创建成功");
    }else
        NSLog(@"文件夹创建失败");
 }
 2.创建文件
 
 
 [Objective-C] 纯文本查看 复制代码 //创建文件
-(void *)createFile{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
    if (res) {
        NSLog(@"文件创建成功: %@" ,testPath);
    }else
        NSLog(@"文件创建失败");
}
 3.写数据到文件
 
 
 [Objective-C] 纯文本查看 复制代码 //写文件
-(void)writeFile{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    NSString *content=@"测试写入内容!";
    BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (res) {
        NSLog(@"文件写入成功");
    }else
        NSLog(@"文件写入失败");
}
 4.读文件数据:
 
 
 [Objective-C] 纯文本查看 复制代码 //读文件
-(void)readFile{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
//    NSData *data = [NSData dataWithContentsOfFile:testPath];
//    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"文件读取成功: %@",content);
}
 5.文件属性
 
 
 [Objective-C] 纯文本查看 复制代码 
//文件属性
-(void)fileAttriutes{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];   
    NSArray *keys;
    id key, value;
    keys = [fileAttributes allKeys];
    int count = [keys count];
    for (int i = 0; i < count; i++)
    {
        key = [keys objectAtIndex: i];
        value = [fileAttributes objectForKey: key];
        NSLog (@"Key: %@ for value: %@", key, value);
    }
}
 6.删除文件
 
 
 [Objective-C] 纯文本查看 复制代码 //删除文件
-(void)deleteFile{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];   
    BOOL res=[fileManager removeItemAtPath:testPath error:nil];
    if (res) {
        NSLog(@"文件删除成功");
    }else
        NSLog(@"文件删除失败");   
    NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
}
 下一篇 应用内购买
 
 
 
 
 
 |