博客> 14. NSFileManager
14. NSFileManager
2018-10-18 06:13 评论:0 阅读:640 Longer_Tao
oc NSFileManager

/********************************** NSFileManager **********************************/
// NSFileManager使用了单例模式
// 使用defaultManager方法可以获得那个单例对象
 [NSFileManager defaultManager];

#pragma mark 1.用法
 // path这个文件\文件夹是否存在
 - (BOOL)fileExistsAtPath:(NSString *)path;

    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);

 // path这个文件\文件夹是否存在, isDirectory代表是否为文件夹
 - (BOOL)fileExistsAtPath:(NSString )path isDirectory:(BOOL )isDirectory;

    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL directory = NO;
    BOOL flag = [manager fileExistsAtPath:@"/Users/LNJ/Desktop/未命名文件夹" isDirectory:&directory];
    NSLog(@"flag = %i, directory = %i", flag, directory);

 // path这个文件\文件夹是否可读
 - (BOOL)isReadableFileAtPath:(NSString *)path;

 // path这个文件\文件夹是否可写,系统目录不允许写入
 - (BOOL)isWritableFileAtPath:(NSString *)path;

 // path这个文件\文件夹是否可删除,系统目录不允许删除
 - (BOOL)isDeletableFileAtPath:(NSString *)path;

#pragma mark 2.文件访问
 // 获得path这个文件\文件夹的属性
 - (NSDictionary )attributesOfItemAtPath:(NSString )path error:(NSError **)error;

    NSFileManager *manager = [NSFileManager defaultManager];
    NSDictionary *dict = [manager attributesOfItemAtPath:@"/Users/LNJ/Desktop/lnj.txt" error:nil];
    NSLog(@"dit = %@", dict);

 // 获得path的当前子路径(?该文件下的其他文件路径?)
 - (NSArray )contentsOfDirectoryAtPath:(NSString )path error:(NSError **)error;

 NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *paths = [manager contentsOfDirectoryAtPath:@"/Users/LNJ/Desktop/" error:nil];
    NSLog(@"paths = %@", paths);

 // 获得文件内容
 - (NSData )contentsAtPath:(NSString )path;

 // 获得path的所有子路径
 - (NSArray )subpathsAtPath:(NSString )path;
 - (NSArray )subpathsOfDirectoryAtPath:(NSString )path error:(NSError **)error;

 NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *paths = [manager subpathsAtPath:@"/Users/LNJ/Desktop/"];
    NSLog(@"paths = %@", paths);

#pragma mark 3.NSFileManager的文件操作
 // 拷贝
 - (BOOL)copyItemAtPath:(NSString )srcPath toPath:(NSString )dstPath error:(NSError **)error;

 // 移动(剪切)
 - (BOOL)moveItemAtPath:(NSString )srcPath toPath:(NSString )dstPath error:(NSError **)error;

 // 删除
 - (BOOL)removeItemAtPath:(NSString )path error:(NSError *)error;

 // 创建文件夹(createIntermediates为YES代表自动创建中间的文件夹)
 - (BOOL)createDirectoryAtPath:(NSString )path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary )attributes error:(NSError **)error;

    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL flag = [manager createDirectoryAtPath:@"/Users/LNJ/Desktop/test" withIntermediateDirectories:YES attributes:nil error:nil];
    NSLog(@"flag = %i", flag);

 // 创建文件(NSData是用来存储二进制字节数据的)
 - (BOOL)createFileAtPath:(NSString )path contents:(NSData )data attributes:(NSDictionary *)attr;

    NSString *str = @"lnj";
    NSData  *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL flag = [manager createFileAtPath:@"/Users/LNJ/Desktop/abc.txt" contents:data attributes:nil];
    NSLog(@"flag = %i", flag);```
收藏
0
sina weixin mail 回到顶部