iOS文件操作

来源:互联网 发布:南红和玛瑙的区别知乎 编辑:程序博客网 时间:2024/05/16 04:05

【原】IOS文件操作

在ios中,所有程序都运行在自己沙箱中,即只能访问自己程序的文件夹,不能访问其他程序的文件夹。 

注意程序文件的路径。

在Application文件夹里面放着所有在模拟器上运行过的程序程序文件。长长的编码是为了唯一地标识一个程序

在每个程序的文件夹里面,Documents文件夹是存放应用程序(app)的地方,而Library文件夹是存放应用程序的设置。

复制代码
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 55, 320, 300)];    webView.delegate = self;    webView.multipleTouchEnabled = YES;    webView.scalesPageToFit = YES;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//搜索本程序文件目录,也就是上面提到的Documents文件夹//NSDocumentDirectory常量就是指定查找的目标是Documents文件夹,而NSUuserDomainMask常量指定从应用程序的主目录中搜索,也就是从Application文件夹的相应程序的文件夹开始找
//YES表示希望获取所有找到的目录的完整路径    NSString *documentsDirectory = [paths objectAtIndex:0];
//这句话记住就行,每个应用程序文件夹里面只有唯一一个Documents文件夹,但是NSSearch方法会返回很多个,我们只取第一个即可。
    NSString *docPath = [documentsDirectory stringByAppendingString:@"/doc2003_1.doc"];    NSLog(@"#######%@",docPath);
    NSURL *url = [NSURL fileURLWithPath:docPath];    NSURLRequest *request = [NSURLRequest requestWithURL:url];    [webView loadRequest:request];
    [self.view addSubview:webView];    [webView release];


序列化:也成为持久化,即将对象永久地从程序中写到文件中,操作方法是

[array writeToFile:filePath atomically:YES];

atomically参数表示文件首先写入一个临时文件中,这种方法保证了文件永远不会损坏,即使写的过程系统崩溃。

对应的是:NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];


摘自:http://www.cnblogs.com/wengzilin/archive/2012/03/28/2421270.html