iOS文件系统---沙盒(sandbox),NSFileManager,NSFileHandle, NSData, 目录,文件

在iOS系统中,每个APP的文件都是独立的, 一个APP大部分处理的文件系统仅限于app的沙盒文件夹.新的APP安装的时候,会产生一系列的文件目录,如下图所示:



下面分开介绍一下:

  AppName.app: 包含app和响应的资源等

  Documents: 储存用户产生的数据,这个文件夹里面的数据能够被用户所操作,能够被itune备份.

  Documents/Inbox:  如果APP允许其他应用打开,这就将其他应用能使用的文件放在这个文件中.

  Library: 里面有几个文件夹, Cache:存放缓存, Preference:存放一些配置信息, NSUserDefault的信息就放在这个文件夹里面. 这里面放的文件是不让用户直接操作的数据. 

   tmp: 临时文件夹,储存一些临时信息,程序退出时会被清除

文件路径:

<pre name="code" class="objc">    //app路径
    NSString *appPath = [NSSearchPathForDirectoriesInDomains(NSApplicationDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    //Document路径
    NSString *documentPath = [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];
    //Library/Preferences路径
    NSString *preferencePath = [libraryPath stringByAppendingPathComponent:@"Preferences"];
    //tmp路径
    NSString *tmpPath = NSTemporaryDirectory();
 
目录相关操作: 

    NSFileManager *fileManager = [NSFileManager defaultManager];
    //判断当前的工作目录
    NSString *currentPath = [fileManager currentDirectoryPath];
    NSLog(@"currentPath = %@",currentPath); //程序启动后默认是跟目录/(程序的主目录)
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    //改变当前目录
    if ([fileManager changeCurrentDirectoryPath:documentPath]) {
        currentPath = [fileManager currentDirectoryPath];
    }
    //创建目录
    NSString *newDirectory = [documentPath stringByAppendingPathComponent:@"newDir"];
    if ([fileManager createDirectoryAtPath:newDirectory withIntermediateDirectories:NO attributes:nil error:nil]) {
        NSLog(@"create success"); //create success
    }
    //列出目录的所有内容
    NSArray *fileContent = [fileManager contentsOfDirectoryAtPath:documentPath error:nil];
    [fileContent enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"obj = %@",obj); //obj = newDir 文件名字
    }];
    //删除目录
    if ([fileManager removeItemAtPath:newDirectory error:nil]) {
        NSLog(@"delete success");//delete succes
    }
    //目录或者文件信息
    NSDictionary *attribute = [fileManager attributesOfItemAtPath:documentPath error:nil];
    NSLog(@"attribute = %@",attribute);
    /*
     attribute = {
     NSFileCreationDate = "2014-11-23 09:48:58 +0000";
     NSFileExtensionHidden = 0;
     NSFileGroupOwnerAccountID = 20;
     NSFileGroupOwnerAccountName = staff;
     NSFileModificationDate = "2014-11-29 12:57:01 +0000";
     NSFileOwnerAccountID = 501;
     NSFilePosixPermissions = 493;
     NSFileReferenceCount = 2;
     NSFileSize = 68;
     NSFileSystemFileNumber = 11768762;
     NSFileSystemNumber = 16777218;
     NSFileType = NSFileTypeDirectory;
     }
     */

文件操作:

    //判断一个文件是否存在
    NSString *filePath = [documentPath stringByAppendingPathComponent:@"newFile.txt"];
    if (![fileManager fileExistsAtPath:filePath]) {
        [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    }
    //列出一个目录中的所有文件
    NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
    NSArray *contents = [fileManager contentsOfDirectoryAtURL:bundleURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
    [contents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"obj = %@",obj);
    }];
    //写入文件
    [@"First Date" writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //或者
    NSData *data = [@"first data" dataUsingEncoding:NSUTF8StringEncoding];
    [data writeToFile:filePath atomically:YES];

    //NSFileHandle 取文件操作柄
    NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
    NSMutableData *orignalData = [NSMutableData dataWithData:[handle readDataToEndOfFile]];
    [orignalData appendData:data];
    [handle closeFile];
    handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    [handle writeData:orignalData];
    [handle closeFile];
    
    //删除文件
    if ([fileManager removeItemAtPath:filePath error:nil]) {
        NSLog(@"delete file success");
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值