iOS基础学习

Objective-C基础学习之NSFileManager

2017-10-26  本文已影响60人  WenJim

1.NSFileManager介绍

  [NSFileManager defaultManager]

2.NSFileManager用法

    NSFileManager *manager = [NSFileManager defaultManager];
    // 可以判断文件
    BOOL flag = [manager fileExistsAtPath:@"/Users/LNJ/Desktop/lnj.txt"];
    NSLog(@"flag = %i", flag);
    // 可以判断文件夹
    flag = [manager fileExistsAtPath:@"/Users/LNJ/Desktop/未命名文件夹"];
    NSLog(@"flag = %i", flag);
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL directory = NO;
    BOOL flag = [manager fileExistsAtPath:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day8/Dem文件保存/File" isDirectory:&directory];
    NSLog(@"flag = %i, directory = %i", flag, directory);

3.NSFileManager的文件访问

    NSFileManager *manager = [NSFileManager defaultManager];
    NSDictionary *dict = [manager attributesOfItemAtPath:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day8/Dem文件保存/File/cwj.txt" error:nil];
    NSLog(@"dit = %@", dict);
    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *paths = [manager contentsOfDirectoryAtPath:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day8/Dem文件保存/File" error:nil];
    NSLog(@"paths = %@", paths);
    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *paths = [manager subpathsAtPath:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day8/Dem文件保存/File/"];
    NSLog(@"paths = %@", paths);

4.NSFileManager的文件操作

    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL flag = [manager createDirectoryAtPath:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day8/Dem文件保存/File" withIntermediateDirectories:YES attributes:nil error:nil];
    NSLog(@"flag = %i", flag);
    NSString *str = @"cwj";
    NSData  *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL flag = [manager createFileAtPath:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day8/Dem文件保存/File/cwj.txt" contents:data attributes:nil];
    NSLog(@"flag = %i", flag);
上一篇 下一篇

猜你喜欢

热点阅读