zoukankan      html  css  js  c++  java
  • IOS管理文件和目录

     

     

    1、常见的NSFileManager文件方法

    -(NSData *)contentsAtPath:path  //从一个文件读取数据

    -(BOOL)createFileAtPath: path contents:(NSData *)data attributes:attr  //向一个文件写入数据

    -(BOOL)removeItemAtPath:path error:err  //删除一个文件

    -(BOOL)moveItemAtPath:from toPath:to error:err  //重命名或者移动一个文件(to不能是已存在的)

    -(BOOL)copyItemAtPath:from toPath:to error:err  //复制文件(to不能是已存在的)

    -(BOOL)contentsEqualAtPath:path andPath:path2  //比较两个文件的内容

    -(BOOL)fileExistAtPath:path  //测试文件是否存在

    -(BOOL)isReadableFileAtPath:path  //测试文件是否存在,并且是否能执行读操作  

    -(BOOL)isWriteableFileAtPath:path  //测试文件是否存在,并且是否能执行写操作  

    -(NSDictionary *)attributesOfItemAtPath:path error:err  //获取文件的属性  

    -(BOOL)setAttributesOfItemAtPath:attr error:err  //更改文件的属性

    2.使用目录

    -(NSString *)currentDirectoryPath  //获取当前目录

    -(BOOL)changeCurrentDirectoryPath:path  //更改当前目录

    -(BOOL)copyItemAtPath:from toPath:to error:err  //复制目录结构(to不能是已存在的)

    -(BOOL)createDirectoryAtPath:path withIntermediateDirectories:(BOOL)flag attribute:attr  //创建一个新目录

    -(BOOL)fileExistAtPath:path isDirectory:(BOOL*)flag  //测试文件是不是目录(flag中储存结果YES/NO)

    -(NSArray *)contentsOfDirectoryAtPath:path error:err  //列出目录内容

    -(NSDirectoryEnumerator *)enumeratorAtPath:path  //枚举目录的内容

    -(BOOL)removeItemAtPath:path error:err  //删除空目录

    -(BOOL)moveItemAtPath:from toPath:to error:err   //重命名或移动一个目录(to不能是已存在的)

    3、常用路径工具方法

    +(NSString *)pathWithComponens:components  //根据components中的元素构造有效路径

    -(NSArray *)pathComponents  //析构路径,获得组成此路径的各个部分

    -(NSString *)lastPathComponent  //提取路径的最后一个组成部分

    -(NSString *)pathExtension  //从路径的最后一个组成部分中提取其扩展名

    -(NSString *)stringByAppendingPathComponent:path  //将path添加到现有路径的末尾

    -(NSString *)stringByAppendingPathExtension:ext  //将指定的扩展名添加到路径的最后一个组成部分

    -(NSString *)stringByDeletingLastPathComponent  //删除路径的最后一个组成部分

    -(NSString *)stringByDeletingPathExtension  //从文件的最后一部分删除扩展名

    -(NSString *)stringByExpandingTileInPath   //将路径中代字符扩展成用户主目录(~)或指定用户的主目录(~user)

    -(NSString *)stringByresolvingSymlinksInPath  //尝试解析路径中的符号链接

    -(NSString *)stringByStandardizingPath  //通过尝试解析~、..(父目录符号)、.(当前目录符号)和符号链接来标准化路径

    4、常用的路径工具函数

    NSString* NSUserName(void)  //返回当前用户的登录名

    NSString* NSFullUserName(void)  //返回当前用户的完整用户名

    NSString* NSHomeDirectory(void)  //返回当前用户主目录的路径

    NSString* NSHomeDirectoryForUser(NSString* user)  //返回用户user的主目录

    NSString* NSTemporaryDirectory(void)  //返回可用于创建临时文件的路径目录

    5、常用的IOS目录

    Documents(NSDocumentDirectory)  //用于写入应用相关数据文件的目录,在ios中写入这里的文件能够与iTunes共享并访问,存储在这里的文件会自动备份到云端

    Library/Caches(NSCachesDirectory)  //用于写入应用支持文件的目录,保存应用程序再次启动需要的信息。iTunes不会对这个目录的内容进行备份

    tmp(use NSTemporaryDirectory())  //这个目录用于存放临时文件,只程序终止时需要移除这些文件,当应用程序不再需要这些临时文件时,应该将其从这个目录中删除

    Library/Preferences  //这个目录包含应用程序的偏好设置文件,使用 NSUserDefault类进行偏好设置文件的创建、读取和修改

    本文转载至:http://www.cnblogs.com/jay-dong/archive/2013/01/21/2870414.html

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

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

     iPhone 文件结构和文件操作

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

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

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

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

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

    相关得一些操作:

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

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

    01 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    02     
    03 NSString *documentsDirectory = [paths objectAtIndex:0];  
    04     
    05 NSLog(@”%@”,documentsDirectory);  
    06     
    07 NSFileManager *fileManage = [NSFileManager defaultManager];  
    08     
    09 NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@“test”];  
    10     
    11 BOOL ok = [fileManage createDirectoryAtPath:myDirectory attributes:nil];


    比如要在Documents下面创建一个file.txt:

      

    // 结果为:/Documents/file.txt 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一样随意写文件。

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

     
    01 bool writeApplicationData(NSData *data, NSString *fileName)  
    02       
    03 {  
    04       
    05     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    06       
    07     NSString *documentsDirectory = [paths objectAtIndex:0];  
    08       
    09     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 }

    本文转载至:http://blog.sina.com.cn/s/blog_51a995b70101ht0l.html

  • 相关阅读:
    解决Android SDK Manager更新、下载速度慢
    selenium报错以及各解决方法
    webdriver对各种浏览器的支持
    selenium driver版本和Chrome浏览器版本对应关系
    selenium web driver
    js中变量注意事项
    js选项卡实现
    两种JS方法实现斐波那契数列
    三种JS方法确定元素在数组中的索引值
    javascript内置属性——arguments
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3370091.html
Copyright © 2011-2022 走看看