OC-文件管理-NSFileManager

2016-06-28  本文已影响894人  风林山火

字数885 阅读10 评论0 喜欢1
需要学习
1.什么是沙盒机制
2.数据持久化的方法
3.常用文件操作类

沙盒(sandbox)机制
它是一种安全体系。它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容。所有的非代码文件都保存在这个地方,比如图片、声音、plist,sqlite数据库及其他文件等。这种“封闭独立安全”的存储空间就是沙盒。

每一个应用程序都有自己独立的沙盒
应用程序不能越过自己的沙盒去访问别人的沙盒
应用程序向外请求或接收数据都需要经过权限认证
模拟器的沙盒文件夹在电脑的存储位置是隐藏的——输入:
/Users/useName/Library/Application/Support/iPhone Simulator
或者在终端输入命令: chflags nohidden~
数据持久化的方法
通常程序在运行中或者程序结束之后,需要保存一些信息,而且需要持久化存储信息,比如登陆信息、视频播放记录、收藏记录等等,那么我们可以采用以下几种方式对数据进行持久化保存.

文件
plist
数据库
常用文件操作类
NSFileManager(文件管理类)
NSFileManager可以用来进行常见的文件\文件夹操作的类(拷贝、剪切、创建、移动、检查文件是否存在、删除、重命名file等)。
OC中系统自带的创建单例对象的方法,一般使用default开头。NSFileManager使用了单例模式singleton。
使用defaultManager方法可以获得那个单例对象,注意不用alloc。
//创建NSFileManager文件管理器类的单例对象
//使用defaultManager方法可以获取单例对象
//fileManager是个单例对象
NSFileManager *fileManager=[NSFileManager defaultManager];
NSLog(@"%p",fileManager);

//NSError是专门用来存储错误信息的。
NSError *error;

//浅度遍历:返回当前目录下,所有的一级目录的文件夹名和文件名        
NSString *path=@"/Users/qingmai/Desktop/file";
NSArray *arr1=[fileManager contentsOfDirectoryAtPath:path error:&error];
NSLog(@"%@",arr1);

//深度遍历:返回当前目录下所有的子文件夹的名和所有的文件名(注意:二级目录以下,返回的时候是相对路径)
NSArray *arr2=[fileManager subpathsOfDirectoryAtPath:path error:&error];
NSLog(@"%@",arr2);

//判断文件(或目录)是否存在
BOOL b1=[fileManager fileExistsAtPath:path];
NSLog(@"%d",b1);

//判断该路径是否是文件夹
BOOL b2; //b2用来记录该路径是否是文件夹
BOOL b3=[fileManager fileExistsAtPath:path isDirectory:&b2];
NSLog(@"%d",b2);                    //判断结果:1

BOOL b4;
NSString *path1=@"/Users/qingmai/Desktop/file/wen.rtf";
BOOL b5=[fileManager fileExistsAtPath:path1 isDirectory:&b4];
NSLog(@"%d",b4);                     //判断结果:0

//创建文件
[fileManager createFileAtPath:path contents:nil attributes:nil];

//创建文件夹
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];

//文件/目录的拷贝
//将file文件夹里的file2文件夹拷贝到file1里
//注意事项:1.不能有空格;2.目标路径里必须包含要拷贝或者移动的文件的名字
NSString *fromPath=@"/Users/qingmai/Desktop/file/file2";
NSString *toPath=@"/Users/qingmai/Desktop/file/file1/file2";
[fileManager copyItemAtPath:fromPath toPath:toPath error:&error];


//文件/目录的移动
//将file文件夹里的wen文件移动到file1文件夹里
NSString *fromPath1=@"/Users/qingmai/Desktop/file/wen.rtf";
NSString *toPath1=@"/Users/qingmai/Desktop/file/file1/wen.rtf";
[fileManager moveItemAtPath:fromPath1 toPath:toPath1 error:nil];

//删除文件/目录
[fileManager removeItemAtPath:toPath1 error:nil];
//打开文件,读取文件内容,并把内容复制到另外一个文件中
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSFileHandle *inFile, *outFile;
        NSData *buffer;
        //打开读取文件
        inFile = [NSFileHandle fileHandleForReadingAtPath:@"testfile.txt"];
        
        if (!inFile) {
            NSLog(@"Open the testfile for reading failed");
            return 1;
        }else{
            NSLog(@"Open the testfile for reading successful");
        }
        //判断文件是否已经存在,如果没有就创建
        if (![[NSFileManager defaultManager] fileExistsAtPath:@"testfile2.txt"]) {
            NSLog(@"testfile2.txt is not existed, and creat it.");
            [[NSFileManager defaultManager] createFileAtPath:@"testfile2.txt" contents:nil attributes:nil];
        }else{
            NSLog(@"testfile2.txt is existed.");
        }
        
        //打开文件写入
        outFile = [NSFileHandle fileHandleForWritingAtPath:@"testfile2.txt"];
        if (outFile) {
            NSLog(@"Open of testout for writing successful");
        }else{
            NSLog(@"Open of testout for writing failed");
            return 2;
        }
        //截断输出文件
        [outFile truncateFileAtOffset:0];
        //从inFile读取数据到缓存中
        buffer = [inFile readDataToEndOfFile];
        //从缓存中读取数据,写入outFile
        [outFile writeData:buffer];
        //关闭两个文件
        [inFile closeFile];
        [outFile closeFile];
        //验证文件内容
        NSLog(@"%@", [NSString stringWithContentsOfFile:@"testfile2.txt" encoding:NSUTF8StringEncoding error:NULL]);
        
    }
    return 0;
}

上一篇下一篇

猜你喜欢

热点阅读