NSSearchPathForDirectoriesInDomains

fileManager的基本应用:查看列表

 

NSArray *NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory,NSSearchPathDomainMask domainMask,BOOL expandTilde);

参数

directory 类型有很多,说几个常用的

    NSLibraryDirectory,         // various documentation, support, and configuration files, resources (Library)
    NSDeveloperDirectory,       // developer resources (Developer) DEPRECATED - there is no one single Developer directory.
    NSUserDirectory,            // user home directories (Users)
    NSDocumentationDirectory,   // documentation (Documentation)
    NSDocumentDirectory,        // documents (Documents)
    NSCachesDirectory           // location of discardable cache files (Library/Caches)

domainMask

    NSUserDomainMask ,         // user's home directory --- place to install user's personal items (~)
    NSLocalDomainMask ,        // local to the current machine --- place to install items available to everyone

 

 

 

 NSFileManager *fileManager=[NSFileManager defaultManager];
 //NSAllDomainsMask 获取到所有目录结构     
 NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSAllDomainsMask , YES);
 

for (NSString *str in documentPaths) { NSLog(@"documentPaths:%@",str); } NSString *documentDir=[documentPaths objectAtIndex:0] ; NSLog(@"documentDir:%@",documentDir);

 

结果如下:

    2013-06-14 16:30:48.253 helloword[1092:c07] documentPaths:/Users/qing/Library/Application Support/iPhone Simulator/6.1/Applications/A3EEDA4B-0F5E-49E5-91D8-FBCCD388B539/Library/Documentation
    2013-06-14 16:30:48.258 helloword[1092:c07] documentPaths:/Library/Documentation
    2013-06-14 16:30:48.258 helloword[1092:c07] documentPaths:/Network/Library/Documentation
    2013-06-14 16:30:48.259 helloword[1092:c07] documentPaths:/System/Library/Documentation
2013-06-14 16:30:48.260 helloword[1092:c07] documentDir:/Users/qing/Library/Application Support/iPhone Simulator/6.1/Applications/A3EEDA4B-0F5E-49E5-91D8-FBCCD388B539/Library/Documentation

 


 

NSSearchPathForDirectoriesInDomains:

NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask , YES);
//documentPaths:/Users/qing/Library/Application Support/iPhone Simulator/6.1/Applications/A3EEDA4B-0F5E-49E5-91D8-FBCCD388B539/Library/Documentation
         
documentPaths=NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory,NSAllDomainsMask , YES); ///Users/qing/Library/Application Support/iPhone Simulator/6.1/Applications/A3EEDA4B-0F5E-49E5-91D8-FBCCD388B539/Downloads


转自 http://higher141125.iteye.com/blog/1886851

 

 

 

 

这个主要就是返回一个绝对路径用来存放我们需要储存的文件。

- (NSString *)dataFilePath {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    return [documentsDirectory stringByAppendingPathComponent:@"shoppingCar.plist"];
}

NSFileManager
* fm=[NSFileManager defaultManager]; if(![fm fileExistsAtPath:[self dataFilePath]]){ //下面是对该文件进行制定路径的保存 [fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil]; //取得一个目录下得所有文件名 NSArray *files = [fm subpathsAtPath: [self dataFilePath] ]; //读取某个文件 NSData *data = [fm contentsAtPath:[self dataFilePath]]; //或者 NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]]; }

 

 
 

转自:http://marshal.easymorse.com/archives/3340

iOS中对文件的操作

因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件:

  • Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录
  • tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
  • Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除

在Documents目录下创建文件

代码如下:

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
                                            , NSUserDomainMask 
                                            , YES); 
NSLog(@"Get document path: %@",[paths objectAtIndex:0]);

NSString *fileName=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"]; 
NSString *content=@"a"; 
NSData *contentData=[content dataUsingEncoding:NSASCIIStringEncoding]; 
if ([contentData writeToFile:fileName atomically:YES]) {
    NSLog(@">>write ok."); 
}

可以通过ssh登录设备看到Documents目录下生成了该文件。

 

上述是创建ascii编码文本文件,如果要带汉字,比如:

NSString *fileName=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"]; 
NSString *content=@"更深夜静人已息"; 
NSData *contentData=[content dataUsingEncoding:NSUnicodeStringEncoding]; 
if ([contentData writeToFile:fileName atomically:YES]) {
    NSLog(@">>write ok."); 
}

如果还用ascii编码,将不会生成文件。这里使用NSUnicodeStringEncoding就可以了。

通过filezilla下载到创建的文件打开,中文没有问题:

image

在其他目录下创建文件

如果要指定其他文件目录,比如Caches目录,需要更换目录工厂常量,上面代码其他的可不变:

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory
                                                , NSUserDomainMask 
                                                , YES);

 

使用NSSearchPathForDirectoriesInDomains只能定位Caches目录和Documents目录。

tmp目录,不能按照上面的做法获得目录了,有个函数可以获得应用的根目录:

NSHomeDirectory()

也就是Documents的上级目录,当然也是tmp目录的上级目录。那么文件路径可以这样写:

NSString *fileName=[NSHomeDirectory() stringByAppendingPathComponent:@"tmp/myFile.txt"];

或者,更直接一点,可以用这个函数:

NSTemporaryDirectory()

不过生成的路径将可能是:

…/tmp/-Tmp-/myFile.txt

使用资源文件

在编写应用项目的时候,常常会使用资源文件,比如:

image

安装到设备上后,是在app目录下的:

image

以下代码演示如何获取到文件并打印文件内容:

NSString *myFilePath = [[NSBundle mainBundle] 
                        pathForResource:@"f" 
                        ofType:@"txt"]; 
NSString *myFileContent=[NSString stringWithContentsOfFile:myFilePath encoding:NSUTF8StringEncoding error:nil]; 
NSLog(@"bundel file path: %@ \nfile content:%@",myFilePath,myFileContent);

 

代码运行效果:

image


 

posted on 2015-01-13 13:31  奋进的闹钟  阅读(870)  评论(0编辑  收藏  举报

导航