【iOS_Development】文件操作

作者: Anticipate_91 | 来源:发表于2017-05-15 09:14 被阅读189次
  • 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];


GitHub主页

CSDN Blog

Email:jinjob@icloud.com

相关文章

  • 【iOS_Development】文件操作

    NSFileManager:是用来管理文件系统的,它可以用来进行常见的文件\文件夹操作 获取NSFileManag...

  • 文件操作

    文件操作 目标 文件操作的作用 文件的基本操作打开读写关闭 文件备份 文件和文件夹的操作 一. 文件操作的作用 思...

  • 文件和目录处理相关

    文件和目录处理相关 题: 考点:文件操作/写入操作; 延伸:目录操作函数,其他文件操作; 文件读写操作 文件系统函...

  • 09-文件操作

    一、文件操作流程 a.普通文件操作流程: 打开文件 操作文件 关闭文件 b. json文件操作流程: open(文...

  • VBS文件操作

    VBS文件操作'操作文本文件,操作fso对象(文件对象操作) --------------------------...

  • 文件操作

    文件操作:打开文件、读写文件、操作文件内容 写入文件操作:(把大象装入冰箱)1.打开文件 ...

  • 类的补充

    一.复习 1.文件操作a.操作流程:打开文件(open),操作文件,关闭文件with open() as 文件变量...

  • 文件

    目标 文件操作的作用 文件的基本操作打开读写关闭 文件备份 文件和文件夹的操作 一. 文件操作的作用 思考:什么是...

  • 16总 正则表达式

    复习: 1.文件操作a.操作流程: 打开文件(open) --- 操作文件 --- 关闭文件(close)with...

  • 2018-09-10

    01-recode 1.文件操作a.操作流程:打开文件---》操作文件----》关闭文件with open() ...

网友评论

  • 秋雨无痕:NSFileHandle这个也可以加上,关于大文件读写

本文标题:【iOS_Development】文件操作

本文链接:https://www.haomeiwen.com/subject/xbwcxxtx.html