在iPhone中读写文件 [英] Read and write file in iPhone

查看:168
本文介绍了在iPhone中读写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iPhone上进行文件读写操作?这是我需要指定保存文件的路径吗?如何在iPhone中获取当前的工作目录?

How to make file read/write operation on iPhone?which is the path i need to specify to save the file? how can i get the current working directory in iPhone?

谢谢

Thank You

推荐答案

写入文件:

Write to a file:

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

// the path to write file
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:@"myFile"];

[data writeToFile:myFile atomically:YES];

阅读文件:

Read a file:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];  
NSData *myData = [NSData dataWithContentsOfFile:filePath];  
if (myData) {  
    // do something useful  
}  

这篇关于在iPhone中读写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆