1.NSFileManager

1.NSFileManager介绍

  • 什么是NSFileManager
    顾名思义, NSFileManager是用来管理文件系统的
    它可以用来进行常见的文件\文件夹操作
    NSFileManager使用了单例模式

  • 使用defaultManager方法可以获得那个单例对象

[NSFileManager defaultManager]

2.NSFileManager用法

  • path这个文件\文件夹是否存在
- (BOOL)fileExistsAtPath:(NSString *)path;

NSFileManager *manager = [NSFileManager defaultManager];
    // 可以判断文件
    BOOL flag = [manager fileExistsAtPath:@"/Users/sss/Desktop/sss.txt"];
    NSLog(@"flag = %i", flag);
    // 可以判断文件夹
    flag = [manager fileExistsAtPath:@"/Users/sss/Desktop/sss"];
    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;

3.NSFileManager的文件访问

  • 获得path这个文件\文件夹的属性
- (NSDictionary )attributesOfItemAtPath:(NSString )path error:(NSError **)error;

        NSDictionary *info = [manager attributesOfItemAtPath:@"/Users/sll/Downloads/Day18/NSFileManager基础/main.m" error:nil];
        NSLog(@"info = %@", info);
  • 获得path的当前子路径
- (NSArray )contentsOfDirectoryAtPath:(NSString )path error:(NSError **)error;
 获取文件夹中所有的文件
        // 注意:contentsOfDirectoryAtPath方法有一个弊端, 只能获取当前文件夹下所有的文件, 不能获取子文件夹下面的文件
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);


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

你可能感兴趣的:(1.NSFileManager)