IOS 开发文件操作——NSFileManager

转自:http://blog.csdn.net/xyz_lmn/article/details/8968213,留着方便查阅

iOS的沙盒机制,应用只能访问自己应用目录下的文件。iOS不像android,没有SD卡概念,不能直接访问图像、视频等内容。iOS应用产生的内容,如图像、文件、缓存内容等都必须存储在自己的沙盒内。默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。

             

上面的完整路径为:用户->资源库->Application Support->iPhone Simulator->6.1->Aplications

Documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;

Library/Caches:存放缓存文件,保存应用的持久化数据,用于应用升级或者应用关闭后的数据保存,不会被itunes同步,所以为了减少同步的时间,可以考虑将一些比较大的文件而又不需要备份的文件放到这个目录下。

tmp:提供一个即时创建临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除。

APP  Sandbox

iOS怎么获取沙盒路径,怎么操作文件呢?下面给出答案。

获取应用沙盒根路径:

[cpp] view plaincopyprint?

  1. -(void)dirHome{
  2. NSString *dirHome=NSHomeDirectory();
  3. NSLog(@"app_home: %@",dirHome);
  4. }

获取Documents目录路径:

[cpp] view plaincopyprint?

  1. //获取Documents目录
  2. -(NSString *)dirDoc{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  4. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  5. NSString *documentsDirectory = [paths objectAtIndex:0];
  6. NSLog(@"app_home_doc: %@",documentsDirectory);
  7. return documentsDirectory;
  8. }

获取Library目录路径:

[cpp] view plaincopyprint?

  1. //获取Library目录
  2. -(void)dirLib{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
  4. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  5. NSString *libraryDirectory = [paths objectAtIndex:0];
  6. NSLog(@"app_home_lib: %@",libraryDirectory);
  7. }

获取Cache目录路径:

[cpp] view plaincopyprint?

  1. //获取Cache目录
  2. -(void)dirCache{
  3. NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4. NSString *cachePath = [cacPath objectAtIndex:0];
  5. NSLog(@"app_home_lib_cache: %@",cachePath);
  6. }

获取Tmp目录路径:

[cpp] view plaincopyprint?

  1. //获取Tmp目录
  2. -(void)dirTmp{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
  4. NSString *tmpDirectory = NSTemporaryDirectory();
  5. NSLog(@"app_home_tmp: %@",tmpDirectory);
  6. }

创建文件夹:

[cpp] view plaincopyprint?

  1. //创建文件夹
  2. -(void *)createDir{
  3. NSString *documentsPath =[self dirDoc];
  4. NSFileManager *fileManager = [NSFileManager defaultManager];
  5. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  6. // 创建目录
  7. BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
  8. if (res) {
  9. NSLog(@"文件夹创建成功");
  10. }else
  11. NSLog(@"文件夹创建失败");
  12. }

创建文件

[cpp] view plaincopyprint?

  1. //创建文件
  2. -(void *)createFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSFileManager *fileManager = [NSFileManager defaultManager];
  6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  7. BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
  8. if (res) {
  9. NSLog(@"文件创建成功: %@" ,testPath);
  10. }else
  11. NSLog(@"文件创建失败");
  12. }

写数据到文件:

[cpp] view plaincopyprint?

  1. //写文件
  2. -(void)writeFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  6. NSString *[email protected]"测试写入内容!";
  7. BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  8. if (res) {
  9. NSLog(@"文件写入成功");
  10. }else
  11. NSLog(@"文件写入失败");
  12. }

读文件数据:

[cpp] view plaincopyprint?

  1. //读文件
  2. -(void)readFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  6. //    NSData *data = [NSData dataWithContentsOfFile:testPath];
  7. //    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
  8. NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
  9. NSLog(@"文件读取成功: %@",content);
  10. }

文件属性:

[cpp] view plaincopyprint?

  1. //文件属性
  2. -(void)fileAttriutes{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSFileManager *fileManager = [NSFileManager defaultManager];
  6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  7. NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
  8. NSArray *keys;
  9. id key, value;
  10. keys = [fileAttributes allKeys];
  11. int count = [keys count];
  12. for (int i = 0; i < count; i++)
  13. {
  14. key = [keys objectAtIndex: i];
  15. value = [fileAttributes objectForKey: key];
  16. NSLog (@"Key: %@ for value: %@", key, value);
  17. }
  18. }

删除文件:

[cpp] view plaincopyprint?

    1. //删除文件
    2. -(void)deleteFile{
    3. NSString *documentsPath =[self dirDoc];
    4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    5. NSFileManager *fileManager = [NSFileManager defaultManager];
    6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    7. BOOL res=[fileManager removeItemAtPath:testPath error:nil];
    8. if (res) {
    9. NSLog(@"文件删除成功");
    10. }else
    11. NSLog(@"文件删除失败");
    12. NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath][email protected]"YES":@"NO");
    13. }
时间: 2024-08-25 19:27:24

IOS 开发文件操作——NSFileManager的相关文章

iOS开发-文件操作

目录操作和文件管理 学习目标 1.理解单例 2.掌握NSFileManager类常用的文件管理操 3.掌握NSFileHandle类常用的文件数据操作 4.了解NSData类的常用操作 5.掌握Plist文件读写 ---------------------- 通常程序在运行中或者程序结束之后,需要保存一些信息,而且需要持久化存储信息,比如登陆信息.视频播放记录.收藏记录等等,那么我们可以采用以下几种方式对数据进行持久化保存. 1.1单例模式(当前对象有且仅有一个实例) 好处:只有一个实例,数据共

iOS——文件操作NSFileManager (创建、删除,复制,粘贴)

iOS——文件操作NSFileManager (创建.删除,复制,粘贴) iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp.Library包含Caches.Preferences目录.               上面的完整路径为:用户->资源库->Applicat

ios开发——实用技术篇&amp;NSFileManager与NSFileHandler

NSFileManager与NSFileHandler 先看图在说话,哈哈! 常用处理 追加数据 每个程序都会有它自己的沙盒,通过它你可以阅读/编写文件.写入沙盒的文件在程序的进程中将会保持稳定,即便实在程序更新的情况下. 如下所示,你可以在沙盒中定位文件目录://对于错误信息NSError *error;// 创建文件管理器NSFileManager *fileMgr = [NSFileManagerdefaultManager];//指向文件目录 NSString *documentsDir

iOS plist 文件操作

转自:http://blog.csdn.net/totogo2010/article/details/7634185 在做iOS开发时,经常用到到plist文件,  那plist文件是什么呢? 它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plist ,因此通常被称为 plist文件.文件是xml格式的. Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息 我们创建一个项目来学习plist文件的读写. 1.创建项目Pl

【非凡程序员】 OC第十七节课 文件操作(NSFileManager和NSFileHandle)

文件操作 #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {    @autoreleasepool {        // insert code here...        NSLog(@"Hello, World!");                //----------- NSFileManager---------//                //实例化

Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作

转自 http://blog.csdn.net/swingpyzf/article/details/15185767 objective-c通过使用NSFileManager类来管理和操作文件.目录,NSFileManager,文件或目录是使用文件的路径名的唯一标示.每个路径名都是一个NSString对象. NSFileManager对象通过defaultManager方法来创建实例 列如: NSFileManager *fm = [NSFileManager defaultManager];

文件操作——NSFileManager

NSFileManager: 对文件本身操作. 创建文件管理对象(单例) #define PATH @"/users/XXX/Desktop/files" NSFileManager * manager = [NSFileManager defaultManager]; 浅遍历 - (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);

IOS之文件操作

Project Day02 文件的相关操作 NSFileManger:对整个文件进行操作<手机文件在沙箱中不能直接操作>   删除文件 步骤: 1 创建NSFileManger的实例 2. 创建NSerror的实例初始值为空 3. 调用removeItemAtPath方法第一个参数为要删除的文件的位置第二个error的参数设置为&error的地址 4. 错误判断 如果出错 可以调用[error  localizedDescription]返回系统提供给的描述 NSFileManager

ios 关于文件操作 获取 文件大小

分类: Apple IPhone2012-06-28 11:31 4664人阅读 评论(0) 收藏 举报 ios语言manager测试c c语言 实现 #include "sys/stat.h" - (long long) fileSizeAtPath:(NSString*) filePath{ struct stat st;      if(lstat([filePath cStringUsingEncoding:NSUTF8StringEncoding], &st) ==