zoukankan      html  css  js  c++  java
  • iphone 文件操作以及文件管理 2

    这篇文章主要是针对沙盒存储方式的文件操作,详文如下:

    对于一个运行在iPhone得app,它只能访问自己根目录下的一些文件(所谓sandbox).

    一个app发布到iPhone上后,它的目录结构如下:

    1、其中得 app root 可以用 NSHomeDirectory() 访问到;
    2、Documents 目录就是我们可以用来写入并保存文件得地方,一般可通过:
    1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    2.   
    3. NSString *documentsDirectory = [paths objectAtIndex:0];  

    得到。

    3、tmp 目录我们可以在里面写入一些程序运行时需要用得数据,里面写入得数据在程序退出后会没有。可以通过

    NSString *NSTemporaryDirectory(void); 方法得到;

    4、文件一些主要操作可以通过NSFileManage 来操作,可以通过 [NSFileManger defaultManger] 得到它得实例。

    相关得一些操作:

     a.创建一个目录或者文件:

    比如要在Documents下面创建一个test目录,

    1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    2.   
    3. NSString *documentsDirectory = [paths objectAtIndex:0];  
    4.   
    5. NSLog(@”%@”,documentsDirectory);  
    6.   
    7. NSFileManager *fileManage = [NSFileManager defaultManager];  
    8.   
    9. NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@“test”];  
    10.   
    11. BOOL ok = [fileManage createDirectoryAtPath:myDirectory attributes:nil];  
    比如要在Documents下面创建一个file.txt:
    1. // Result is: /Documents/file1.txt结果为:/Documents/file.txt  
    2. NSString *filePath= [documentsDirectory stringByAppendingPathComponent:@"file.txt"];  
     b.取得一个目录下得所有文件名:
    1. //如上面的myDirectory)可用  
    2. NSArray *file = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];  
    3.   
    4. 或  
    5.   
    6. NSArray *files = [fileManager subpathsAtPath: myDirectory ];  

     c.读取某个文件:

    1. NSData *data = [fileManger contentsAtPath:myFilePath];//myFilePath是包含完整路径的文件名  
    2.   
    3. 或直接用NSData 的类方法:  
    4.   
    5. NSData *data = [NSData dataWithContentOfPath:myFilePath];  

     d.保存某个文件:

    1. //可以用 NSFileManager的下列方法:  
    2.   
    3. - (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;  
    4.   
    5. 或 NSData 的  
    6.   
    7. - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;  
    8.   
    9. - (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr;  

      e.删除某个文件:

    1. //可以用 NSFileManager的下列方法:  
    2.   
    3. //Removes the file or directory at the specified path.  
    4. - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error  
    5.   
    6. //Removes the file or directory at the specified URL.  
    7. - (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error  

     f.移动某个文件或者重命名某文件

    1. 想要重命名一个文件,我们需要把文件移到一个新的路径下。下面的代码创建了我们所期望的目标文件的路径,然后请求移动文件以及在移动之后显示文件目录。  
    2. //通过移动该文件对文件重命名  
    3. NSString *filePath2= [documentsDirectory stringByAppendingPathComponent:@"file2.txt"];  
    4. //判断是否移动  
    5. if ([fileManager moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)  
    6. NSLog(@"Unable to move file: %@", [error localizedDescription]);  
    7. //显示文件目录的内容  
    8. NSLog(@"Documentsdirectory: %@",[fileManager contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);  
    
    

    iPhone官方SDK用于读写临时数据的方法

    我们知道,出于安全考虑,iPhone的官方SDK并不能像toolchain一样随意写文件。

    感谢waza提供的官方SDK用于读写临时数据的方法。

    注意:这两个方法都是存储在/Documents/里面。

    1. bool writeApplicationData(NSData *data, NSString *fileName)  
    2.     
    3. {  
    4.     
    5.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    6.     
    7.     NSString *documentsDirectory = [paths objectAtIndex:0];  
    8.     
    9.     if (!documentsDirectory) {  
    10.     
    11.         NSLog(@"Documents directory not found!");  
    12.     
    13.         return NO;  
    14.     
    15.        }  
    16.     
    17.     NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];  
    18.    
    19.     return ([data writeToFile:appFile atomically:YES]);  
    20.    
    21. }  
    22.    
    23.            
    24.    
    25. NSData *applicationDataFromFile(NSString *fileName)  
    26.    
    27. {  
    28.    
    29.    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    30.    
    31.    NSString *documentsDirectory = [paths objectAtIndex:0];  
    32.    
    33.    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];  
    34.    
    35.    NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];  
    36.    
    37.    return myData;  
    38. }  
  • 相关阅读:
    mysql 实战 or、in与union all 的查询效率
    转:手机流畅的决定性因素
    合批只是对CPU的优化,与GPU没有任何关系
    UNITY 打包时提示sdk tools 或 sdk build tools版本低时可以直接点update 按钮进行更新
    RGB ECT 4BIT 压缩后质量远高于RGB ETC2 4BIT
    Adreno GPU Profiler
    UNITY2018.3 在editor下运行时new memoryprofiler显示 shader占用内存很大的问题在安卓上并没有看到
    VS2017断点调试UNITY2018.3 经常卡住的问题
    一次UNITY闪退问题的定位心得
    UNITY2018 真机开启deepprofiling的操作
  • 原文地址:https://www.cnblogs.com/careerman/p/2648891.html
Copyright © 2011-2022 走看看