NSFileManager的使用

废话不多说, 直接上代码吧  


- (void)test {
    
    // 获取NSFileManager实例
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 获取应用沙盒的目录
    /**
     *  @param 参数1 常用的有三种:
         1.NSDocumentDirectory, document目录
         2.NSCachesDirectory,  缓存目录
         3.NSLibraryDirectory
     *  @param 参数2  一般直接用这个NSUserDomainMask
     *  @param 参数3  此参数意思为是否展开目录 ,一般为YES
     *
     *
     */
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    
    //在documenPath下拼接一个test的路径
    NSString *path = [documentPath stringByAppendingPathComponent:@"test"];
    //创建文件夹test
    [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    NSLog(@"%@", path);
    // 在test下拼接一个test.txt的文件名
    NSString *testPath = [path stringByAppendingPathComponent:@"test1.txt"];
    //[testPath writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    //文件写入练习    写入字符串
    [[NSFileManager defaultManager] createFileAtPath:testPath contents:[@"hellow  world" dataUsingEncoding:NSUTF8StringEncoding]  attributes:nil];
    
    //搜索指定目录下所有文件: 两种方式都可以
    
    NSArray *files = [fileManager subpathsAtPath:documentPath];
    NSLog(@"%@",files);
    /*  打印结果
     2016-05-08 17:01:05.055 abc[26138:582734] (
     test,
     "test/.DS_Store",
     "test/test.c",
     "test/test.txt",
     "test/test0.txt",
     "test/test1.txt",
     test5
     )
     */
    
    NSArray *files1 = [fileManager subpathsOfDirectoryAtPath:documentPath error:nil];
    
    NSLog(@"%@",files1);
    /*  打印结果
     2016-05-08 17:01:05.055 abc[26138:582734] (
     test,
     "test/.DS_Store",
     "test/test.c",
     "test/test.txt",
     "test/test0.txt",
     "test/test1.txt",
     test5
     )
     */

    //删除指定目录的文件
    [fileManager removeItemAtPath:documentPath error:nil];
    
}
//混合数据的读写
- (void)test1 {
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    
    //在documenPath下拼接一个test的路径
    NSString *path = [documentPath stringByAppendingPathComponent:@"test1.txt"];
    
    //穿件不同数据
    NSString *temp = @"hello world";
    int dataInt = 111;
    float dataFloat = 1.115f;
    NSLog(@"----------%.2f", dataFloat);
    //创建一个临时数据存储变量
    NSMutableData *dataM = [[NSMutableData alloc] init];
    
    
    //将数据拼接到dataM
    [dataM appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
    [dataM appendBytes:&dataInt length:sizeof(dataInt)];
    [dataM appendBytes:&dataFloat length:sizeof(dataFloat)];
    
    //写入数据
    [dataM writeToFile:path atomically:YES];
    
    
    /***************以下为读取部分*****************/
    int intData;
    float floatData = 0.0;
    NSString *stringData;
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    stringData = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, [temp length])] encoding:NSUTF8StringEncoding];
    [data getBytes:&intData range:NSMakeRange([temp length], sizeof(intData))];
    
    [data getBytes:&floatData range:NSMakeRange([temp length] + sizeof(intData), sizeof(floatData))];
    NSLog(@"%@, %d, %.2f", temp, intData, floatData);

    
    

}


你可能感兴趣的:(文件管理,沙盒文件)