iOS - 沙盒路径(sandbox)和文件操作

沙盒机制

  • iOS应用程序只能读取自己创建的文件,这个"独立",“封闭”,"安全"的空间,称之为沙盒,所有的非代码文件都要保存在此。
  • 它一般存放着你程序包文件(可执行文件)、图片、声音、视频、plist、sqlite数据库以及其他文件。
  • 每个应用程序都有自己的独立的存储空间(沙盒),一般情况下应用程序是不可以相互访问沙盒,也有例外,比如系统通讯录能在用户授权的情况下被第三方应用访问。

沙盒路径

  • Documnets:一般我们需要持久的数据都放在这个目录中,你可以在当中添加子文件夹,需要注意的是,Documents 存放不会被删除的文件,iTunes备份和恢复的时候,会包括此目录,不能放太大的文件.不然苹果审核会被拒掉。
  • Library:设置程序的默认设置和其他状态信息。Library/Caches:存放缓存文件,应用程序再次启动过程中需要的信息,Caches (缓存) 一般放体积比较大,不重要的资源,itenus不会备份。Library/Preferences:包含应用程序的偏好设置文件,preferences 不要随意动他,通过使用NSUserDefault类取得应用程序的偏好,itunes会备份。
  • tmp:创建临时文件的目录,tmp保存应用的临时数据,itunes不会同步该目录,长时间不用可能会被程序删掉,重启iphone时会被删掉。

路径操作

描述 操作
获取沙盒目录 NSString *homePath = NSHomeDirectory()
获取Documnets路径 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
获取Library路径 NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES) objectAtIndex:0]
获取Library中的Cache NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0]
获取tmp路径 NSString *tempPath = NSTemporaryDirectory()
获取到程序包路径 NSString *boundlePaht = [[NSBundle mainBundle] resourcePath]

文件操作

获取文件数据

	NSData *data = [fileManger contentsAtPath:filePath];
	//通过NSFileHandle获取文件数据
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
    NSData *data = [fileHandle readDataToEndOfFile]; 

追加数据

    NSString *homePath = NSHomeDirectory();
    NSString *filePath = [homePath stringByAppendingPathComponent:@"test.txt"];
    NSString *contentStr = @"this is test";
    NSData *contentData = [contentStr dataUsingEncoding:NSUTF8StringEncoding];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager createFileAtPath:filePath contents:contentData attributes:nil];
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
    //指定在哪个位置追加数据
    [fileHandle seekToEndOfFile];
    NSString *str = @"new data";
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    //利用fileHandle向文件写入数据
    [fileHandle writeData:data];
    //关闭文件
    [fileHandle closeFile];

读取文件数据

    NSString *homePath = NSHomeDirectory();
    NSString *filePath = [homePath stringByAppendingPathComponent:@"test.txt"];
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
    //获取到文件中可用数据的大小
    NSUInteger length = [fileHandle availableData].length;
    //指定从哪里开始读取
    [fileHandle seekToFileOffset:length/2];//断点续传会保留文件偏移量
    //读取数据
    NSData *data = [fileHandle readDataToEndOfFile];
    //将data转化为string
    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);

输入输出流

    NSString *homePath = NSHomeDirectory();
    NSString *sourcePath = [homePath stringByAppendingPathComponent:@"test.txt"];
    NSString *tagetPath = [homePath stringByAppendingPathComponent:@"test2.txt"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //创建目录
    //[fileManager createDirectoryAtPath:@"" withIntermediateDirectories:YES attributes:nil error:nil];
    [fileManager createFileAtPath:tagetPath contents:nil attributes:nil];
    //通过outFileHandle 写入
    NSFileHandle *outFileHandle = [NSFileHandle fileHandleForWritingAtPath:tagetPath];
    //通过inFileHandle 读取
    NSFileHandle *inFileHandle = [NSFileHandle fileHandleForReadingAtPath:sourcePath];
    NSData *data = [inFileHandle readDataToEndOfFile];
    [outFileHandle writeData:data];
    [outFileHandle closeFile];
    [inFileHandle closeFile];

获取文件夹下的所有文件名

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *homePath = NSHomeDirectory();
    NSString *dirPath = [homePath stringByAppendingPathComponent:@"test"];
    NSDirectoryEnumerator *files = [fileManager enumeratorAtPath:dirPath];
    NSString *path = [files nextObject];
    NSInteger fileNum = 0;
    while (path != nil){
        //通过attributesOfItemAtPath获取指定路径文件信息
        NSDictionary *attrDic = [fileManager attributesOfItemAtPath:dirPath error:nil];
        NSNumber *fileSize = attrDic[NSFileSize];
        NSLog(@"%d", [fileSize intValue]);
        fileNum++;
        //取出来下一个文件
        path = [files nextObject];
    }
    NSLog(@"%ld",fileNum);

移动文件

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *targetPaht = NSHomeDirectory();
    NSString *dirPath = [targetPaht stringByAppendingPathComponent:@"test"];
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"test.txt"];
    targetPaht = [targetPaht stringByAppendingPathComponent:@"test.txt"];
    BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:targetPaht error:nil];
    if (isSuccess == YES) {
        NSLog(@"移动成功");
    }

复制文件

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *homeFilePath = NSHomeDirectory();
    NSString *dirPath = [homeFilePath stringByAppendingPathComponent:@"test"];
    NSString *filePath = [dirPath stringByAppendingPathComponent:@"test.txt"];
    homeFilePath = [homeFilePath stringByAppendingPathComponent:@"test.txt"];
    BOOL isSuccess = [fileManager copyItemAtPath:filePath toPath:homeFilePath error:nil];
    if (isSuccess == YES) {
        NSLog(@"复制成功");
    }

删除文件

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *homeFilePath = NSHomeDirectory();
    homeFilePath = [homeFilePath stringByAppendingPathComponent:@"test.txt"];
    BOOL isSuccess = [fileManager removeItemAtPath:homeFilePath error:nil];
    if (isSuccess == YES) {
        NSLog(@"删除成功");
    }
发布了38 篇原创文章 · 获赞 5 · 访问量 9049

猜你喜欢

转载自blog.csdn.net/zj382561388/article/details/103173627
今日推荐