时间:2017年06月06日星期二
说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com
教学示例源码:无
个人学习源码:https://github.com/zccodere/s...

第一章:学习指南

1-1 学习指南

应用需求

clipboard.png

学习目标

1.文件读写的应用场景
2.了解iOS的文件存储机制
3.使用基本的文件读写进行开发

第二章:沙盒介绍

2-1 沙盒介绍

沙盒机制示意图

clipboard.png

应用只能访问自己应用目录下的文件。
沙盒机制

1.Documents:程序创建或应用浏览产生的文件数据
2.Library:程序的默认设置或状态信息保存在该目录
3.tmp:提供一个即时创建临时文件的地方,但不需要持久化

文件目录示意图

clipboard.png

路径处理【重点】

1.沙盒路径的处理
2.NSString类路径函数的处理
3.关于NSData

2-2 获取Documents路径

代码演示:

// 获取沙盒的路径
- (NSString *)getHomePath{
    NSString *homePath = NSHomeDirectory();
    NSLog(@"HomePath = %@",homePath);
    return homePath;
}

// 获取 Documents 路径
- (NSString *)getDocumentsPath{
    // 在应用中检索指定的路径
    // 第一个参数:搜索的路径名称
    // 第二个参数:限定在沙盒内
    NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [docPaths lastObject];
    NSLog(@"DocumentPath = %@",documentPath);
    return documentPath;
}

2-3 获取Library和Tmp路径

代码演示:

// 获取 Library 路径
- (NSString *)getLibraryPath{
    NSArray *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryPath = [libPaths firstObject];
    NSLog(@"LibraryPath = %@",libraryPath);
    return libraryPath;
}

// 获取 Tmp 路径
- (NSString *)getTmpPath{
    NSString *tmpPath = NSTemporaryDirectory();
    NSLog(@"TmpPath = %@",tmpPath);
    return tmpPath;
}

第三章:数据的转换

3-1 NSString类的路径函数的处理

代码演示:

// NSString 类的路径函数的处理
- (void)parsePath {
    NSString *path = @"/data/Containers/Data/Application/test.png";
    // 获得路径的各个组成部分
    NSArray *array = [path pathComponents];
    NSLog(@"PathComponents = %@",array);
    // 提取路径的最后一个组成部分
    NSString *name = [path lastPathComponent];
    NSLog(@"FileName = %@",name);
    // 删除路径的最后一个组成部分
    NSString *string = [path stringByDeletingLastPathComponent];
    NSLog(@"LastPath = %@",string);
    // 追加 name.txt
    NSString *addString = [string stringByAppendingPathComponent:@"name.txt"];
    NSLog(@"AddString = %@",addString);
}

3-2 NSData数据转换

代码演示:

// NSData 数据转换
- (void) dataChange:(NSData *)data {
    
    // NSData -> NSString
    NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    // NSString -> NSData
    NSData *aData = [aString dataUsingEncoding:NSUTF8StringEncoding];
    
    // NSData -> UIImage
    UIImage *image = [UIImage imageWithData:aData];
    
    // UIImage -> NSData
    NSData *data2 = UIImagePNGRepresentation(image);
    NSLog(@"%@",data2);
    
}

第四章:文件操作详解

4-1 使用NSFileManager创建文件夹

文件操作函数

1.NSFileHandle类主要对文件内容进行读取和写入操作
2.NSFileManager类主要对文件的操作(删除、创建等等)

代码演示:

// 创建文件夹
- (void)createFolder {
    NSString *docPath = [self getDocumentsPath];
    NSString *testPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    // withIntermediateDirectories YES 可以覆盖 NO 不可以覆盖
    BOOL result = [manager createDirectoryAtPath:testPath withIntermediateDirectories:YES attributes:nil error:nil];
    if (result) {
        NSLog(@"文件夹创建成功");
    } else{
        NSLog(@"文件夹创建失败");
    }
}

// 创建文件
- (void)createFile {
    NSString *docPath = [self getDocumentsPath];
    NSString *dirPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"我的笔记.txt"];
    NSFileManager *manager = [NSFileManager defaultManager];
    
    BOOL result = [manager createFileAtPath:filePath contents:nil attributes:nil];
    if (result) {
        NSLog(@"文件创建成功");
    } else{
        NSLog(@"文件创建失败");
    }
}

4-2 写入文件

代码演示:

// 写入文件
- (void)writeFile {
    NSString *docPath = [self getDocumentsPath];
    NSString *dirPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"我的笔记.txt"];
    
    NSString *content = @"测试写笔记";
    
    BOOL result = [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (result) {
        NSLog(@"文件写入成功");
    } else{
        NSLog(@"文件写入失败");
    }
    
}

4-3 追加文件内容

代码演示:

// 追加文件内容
- (void) addFileContent{
    NSString *docPath = [self getDocumentsPath];
    NSString *dirPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"我的笔记.txt"];
    
    // 打开文件,准备更新
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
    // 将节点跳到文件的末尾
    [fileHandle seekToEndOfFile];
    NSString *addContent = @"这是我要追加上去的内容";
    NSData *addContentData = [addContent dataUsingEncoding:NSUTF8StringEncoding];
    // 写入数据
    [fileHandle writeData:addContentData];
    // 关闭文件
    [fileHandle closeFile];
}

4-4 删除文件

代码演示:

// 判断指定文件是否存在
- (BOOL)fileExist:(NSString *)filePath{
    NSFileManager *manager = [NSFileManager defaultManager];
    if([manager fileExistsAtPath:filePath]){
        return YES;
    }
    return NO;
}

// 删除文件
- (void)removeFile{
    NSString *docPath = [self getDocumentsPath];
    NSString *dirPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"我的笔记.txt"];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    BOOL isExist = [self fileExist:filePath];
    if(isExist){
        BOOL result = [manager removeItemAtPath:filePath error:nil];
        if (result) {
            NSLog(@"文件删除成功");
        }else{
            NSLog(@"文件删除失败");
        }
    }else{
        NSLog(@"文件不存在。文件路径 = %@",filePath);
        return;
    }
}

第五章:实例讲解

5-1 实例操作

实例说明

1.获取图片存入本地指定文件夹
2.去读本地指定文件夹图片进行展示【课后练习】

代码演示:

// 写入图片
- (void)writeImage{
    UIImage *testImage = [UIImage imageNamed:@"1.jpg"];
    NSData *imageData = UIImagePNGRepresentation(testImage);
    
    NSString *docPath = [self getDocumentsPath];
    NSString *dirPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"图片"];
    
    [imageData writeToFile:filePath atomically:YES];
}

第六章:课程总结

6-1 课程总结

课程回顾

应用场景
沙盒机制
路径处理
文件处理实例

本节课程完整代码

//
//  ViewController.m
//  MyPathFunc
//
//  Created by zc on 2017/6/6.
//  Copyright © 2017年 com.zccoder. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    [self getHomePath];
//    [self getDocumentsPath];
//    [self getLibraryPath];
//    [self getTmpPath];
//    [self parsePath];
//    [self createFolder];
//    [self createFile];
//    [self writeFile];
//    [self addFileContent];
//    [self removeFile];
    [self writeImage];
}

// 获取沙盒的路径
- (NSString *)getHomePath{
    NSString *homePath = NSHomeDirectory();
    NSLog(@"HomePath = %@",homePath);
    return homePath;
}

// 获取 Documents 路径
- (NSString *)getDocumentsPath{
    // 在应用中检索指定的路径
    // 第一个参数:搜索的路径名称
    // 第二个参数:限定在沙盒内
    NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [docPaths lastObject];
    NSLog(@"DocumentPath = %@",documentPath);
    return documentPath;
}

// 获取 Library 路径
- (NSString *)getLibraryPath{
    NSArray *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryPath = [libPaths firstObject];
    NSLog(@"LibraryPath = %@",libraryPath);
    return libraryPath;
}

// 获取 Tmp 路径
- (NSString *)getTmpPath{
    NSString *tmpPath = NSTemporaryDirectory();
    NSLog(@"TmpPath = %@",tmpPath);
    return tmpPath;
}

// NSString 类的路径函数的处理
- (void)parsePath {
    NSString *path = @"/data/Containers/Data/Application/test.png";
    // 获得路径的各个组成部分
    NSArray *array = [path pathComponents];
    NSLog(@"PathComponents = %@",array);
    // 提取路径的最后一个组成部分
    NSString *name = [path lastPathComponent];
    NSLog(@"FileName = %@",name);
    // 删除路径的最后一个组成部分
    NSString *string = [path stringByDeletingLastPathComponent];
    NSLog(@"LastPath = %@",string);
    // 追加 name.txt
    NSString *addString = [string stringByAppendingPathComponent:@"name.txt"];
    NSLog(@"AddString = %@",addString);
}

// NSData 数据转换
- (void) dataChange:(NSData *)data {
    
    // NSData -> NSString
    NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    // NSString -> NSData
    NSData *aData = [aString dataUsingEncoding:NSUTF8StringEncoding];
    
    // NSData -> UIImage
    UIImage *image = [UIImage imageWithData:aData];
    
    // UIImage -> NSData
    NSData *data2 = UIImagePNGRepresentation(image);
    NSLog(@"%@",data2);
    
}

// 创建文件夹
- (void)createFolder {
    NSString *docPath = [self getDocumentsPath];
    NSString *testPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    // withIntermediateDirectories YES 可以覆盖 NO 不可以覆盖
    BOOL result = [manager createDirectoryAtPath:testPath withIntermediateDirectories:YES attributes:nil error:nil];
    if (result) {
        NSLog(@"文件夹创建成功");
    } else{
        NSLog(@"文件夹创建失败");
    }
}

// 创建文件
- (void)createFile {
    NSString *docPath = [self getDocumentsPath];
    NSString *dirPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"我的笔记.txt"];
    NSFileManager *manager = [NSFileManager defaultManager];
    
    BOOL result = [manager createFileAtPath:filePath contents:nil attributes:nil];
    if (result) {
        NSLog(@"文件创建成功");
    } else{
        NSLog(@"文件创建失败");
    }
}

// 写入文件
- (void)writeFile {
    NSString *docPath = [self getDocumentsPath];
    NSString *dirPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"我的笔记.txt"];
    
    NSString *content = @"测试写笔记";
    
    BOOL result = [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (result) {
        NSLog(@"文件写入成功");
    } else{
        NSLog(@"文件写入失败");
    }
    
}

// 判断指定文件是否存在
- (BOOL)fileExist:(NSString *)filePath{
    NSFileManager *manager = [NSFileManager defaultManager];
    if([manager fileExistsAtPath:filePath]){
        return YES;
    }
    return NO;
}

// 追加文件内容
- (void) addFileContent{
    NSString *docPath = [self getDocumentsPath];
    NSString *dirPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"我的笔记.txt"];
    
    // 打开文件,准备更新
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
    // 将节点跳到文件的末尾
    [fileHandle seekToEndOfFile];
    NSString *addContent = @"这是我要追加上去的内容";
    NSData *addContentData = [addContent dataUsingEncoding:NSUTF8StringEncoding];
    // 写入数据
    [fileHandle writeData:addContentData];
    // 关闭文件
    [fileHandle closeFile];
}

// 删除文件
- (void)removeFile{
    NSString *docPath = [self getDocumentsPath];
    NSString *dirPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"我的笔记.txt"];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    BOOL isExist = [self fileExist:filePath];
    if(isExist){
        BOOL result = [manager removeItemAtPath:filePath error:nil];
        if (result) {
            NSLog(@"文件删除成功");
        }else{
            NSLog(@"文件删除失败");
        }
    }else{
        NSLog(@"文件不存在。文件路径 = %@",filePath);
        return;
    }
}

// 写入图片
- (void)writeImage{
    UIImage *testImage = [UIImage imageNamed:@"1.jpg"];
    NSData *imageData = UIImagePNGRepresentation(testImage);
    
    NSString *docPath = [self getDocumentsPath];
    NSString *dirPath = [docPath stringByAppendingPathComponent:@"慕课网"];
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"图片"];
    
    [imageData writeToFile:filePath atomically:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

妙手空空
1.3k 声望368 粉丝

博观而约取,厚积而薄发