【iOS_Development】文件操作

iOS 文件操作 —— 由 anticipate_91分享
  • NSFileManager:是用来管理文件系统的,它可以用来进行常见的文件\文件夹操作
  • 获取NSFileManager示例[NSFileManager defaultManager]

增删改查

1. 创建文件夹

- (void)createFolder {
    // 获取documentsPath
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    // 文件夹路径
    NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"];

    NSError *error;
    BOOL temp = [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
    if (temp) {
        NSLog(@"文件夹创建成功:%@", folderPath);
    } else {
        NSLog(@"文件夹创建失败\n失败原因:%@", error);
    }
}

2. 创建文件

- (void)createFile {
    // 获取documentsPath
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    // 设置文件夹路径
    NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"];

    NSString *testPath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"];
    BOOL temp = [[NSFileManager defaultManager] createFileAtPath:testPath contents:nil attributes:nil];
    if (temp) {
        NSLog(@"文件创建成功:%@", testPath);
    } else {
        NSLog(@"文件创建失败");
    }
}

3. 向文件中写入数据

- (void)writeDataToFile {
    // 获取documentsPath
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    // 文件夹路径
    NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"];
    // 文件路径
    NSString *filePath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"];

    NSString *content = @"hello world";
    NSError *error;
    BOOL temp = [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (temp) {
        NSLog(@"文件写入成功:%@", filePath);
    } else {
        NSLog(@"文件写入失败\n失败原因:%@", error);
    }
}

4. 删除文件

- (void)deleteFile {
    // 获取documentsPath
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    // 文件夹路径
    NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"];
    // 文件路径
    NSString *filePath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"];

    NSError *error;
    BOOL temp = [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
    if (temp) {
        NSLog(@"文件删除成功");
    } else {
        NSLog(@"文件删除失败\n失败原因:%@", error);
    }
}

5. 从文件中读取数据

- (void)readFile {
    // 获取documentsPath
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    // 文件夹路径
    NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"];
    // 文件路径
    NSString *filePath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"];

//    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSError *error;
    NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
    if (!error) {
        NSLog(@"文件读取成功:%@", content);
    } else {
        NSLog(@"文件写入失败\n失败原因:%@", error);
    }
}

常用工具方法

1. 判断文件是否存在

[[NSFileManager defaultManager] fileExistsAtPath:filePath]

2. 判断是否为一个目录

[[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir];

3. 判断文件是否可读

[[NSFileManager defaultManager] isReadableFileAtPath:filePath];

4. 是否可写

[[NSFileManager defaultManager] isWritableFileAtPath:filePath];

5. 是否可删除

[[NSFileManager defaultManager] isDeletableFileAtPath:filePath];

6. 获取文件属性

NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];

7. copy文件

[[NSFileManager defaultManager] copyItemAtPath:path1 toPath:path2 error:nil];

8. 移动文件

[[NSFileManager defaultManager] moveItemAtPath:createDirPath toPath:targetPath error:nil];NSF
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值