IOS 文件读写操作详解及简单实例
程序员文章站
2023-12-20 12:11:28
iphone 文件读写操作
1,写文件操作
- (ibaction)btnwrite:(id)sender {
//创建文件管理器
nsfile...
iphone 文件读写操作
1,写文件操作
- (ibaction)btnwrite:(id)sender { //创建文件管理器 nsfilemanager *filemanager = [nsfilemanager defaultmanager]; //获取路径 //参数 要获取的哪种路径 nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentdirectory = [paths objectatindex:0];//去除需要的路径 //更改到操作的目录下 [filemanager changecurrentdirectorypath:[documentdirectory stringbyexpandingtildeinpath]]; //创建文件filename文件名称,contents文件内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil [filemanager createfileatpath:@"filename" contents:nil attributes:nil]; //删除待删除文件 [filemanager removeitematpath:@"createnewfile" error:nil]; //获取文件路径 nsstring *path = [documentdirectory stringbyappendingpathcomponent:@"filename"]; nslog(@"path == %@",path); //待写入的数据 nsstring *temp = @"hello world"; int data0 = 1000000; float data2 = 23.23f; //创建数据缓冲 nsmutabledata *writer = [[nsmutabledata alloc]init]; //将字符串添加到缓冲中 [writer appenddata:[temp datausingencoding:nsutf8stringencoding]]; [writer appendbytes:&data0 length:sizeof(data0)]; [writer appendbytes:&data2 length:sizeof(data2)]; [writer writetofile:path atomically:yes]; [writer release]; }
2,读文件操作
- (ibaction)btnread:(id)sender { //创建文件管理器 nsfilemanager *filemanager = [nsfilemanager defaultmanager]; //获取路径 //参数 要获取的哪种路径 nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentdirectory = [paths objectatindex:0];//去除需要的路径 //更改到操作的目录下 [filemanager changecurrentdirectorypath:[documentdirectory stringbyexpandingtildeinpath]]; //创建文件filename文件名称,contents文件内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil [filemanager createfileatpath:@"filename" contents:nil attributes:nil]; //删除待删除文件 [filemanager removeitematpath:@"createnewfile" error:nil]; //获取文件路径 nsstring *path = [documentdirectory stringbyappendingpathcomponent:@"filename"]; nslog(@"path == %@",path); //待写入的数据 nsstring *temp = @"hello world"; int data0 = 1000000; float data2 = 23.23f; //创建数据缓冲 nsmutabledata *writer = [[nsmutabledata alloc]init]; //将字符串添加到缓冲中 [writer appenddata:[temp datausingencoding:nsutf8stringencoding]]; [writer appendbytes:&data0 length:sizeof(data0)]; [writer appendbytes:&data2 length:sizeof(data2)]; [writer writetofile:path atomically:yes]; [writer release]; //读取操作 int gdata0; float gdata1; nsstring *gdata2; nsdata *reader = [nsdata datawithcontentsoffile:path]; gdata2 = [[nsstring alloc]initwithdata:[reader subdatawithrange:nsmakerange(0, [temp length])] encoding:nsutf8stringencoding]; [reader getbytes:&gdata0 range:nsmakerange([temp length], sizeof(gdata0))]; [reader getbytes:&gdata1 range:nsmakerange([temp length]+ sizeof(gdata0) , sizeof(gdata1))]; nslog(@"gdata0 == %d",gdata0); nslog(@"gdata1 == %f",gdata1); nslog(@"gdata2 == %@",gdata2); // lbltext.text = gdata2; }
iphone实现文件的读写操作
iphone可以很容易的实现文件的读写,但是如果你没有使用苹果公司99美元开发者sdk的话,你利用破解打出来的api包,安装到手机上不能把文件写入手机.下面是我实现读写的代码:
#import "managerfile.h" @implementation managerfile -(void)writefile:(nsstring *)file { //创建文件管理器 nsfilemanager *filemanager = [nsfilemanager defaultmanager]; //获取路径 //参数nsdocumentdirectory要获取那种路径 nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0];//去处需要的路径 //更改到待操作的目录下 [filemanager changecurrentdirectorypath:[documentsdirectory stringbyexpandingtildeinpath]]; //创建文件filename文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil //获取文件路径 [filemanager removeitematpath:@"username" error:nil]; nsstring *path = [documentsdirectory stringbyappendingpathcomponent:@"username"]; //创建数据缓冲 nsmutabledata *writer = [[nsmutabledata alloc] init]; //将字符串添加到缓冲中 [writer appenddata:[file datausingencoding:nsutf8stringencoding]]; //将其他数据添加到缓冲中 //将缓冲的数据写入到文件中 [writer writetofile:path atomically:yes]; [writer release]; } -(nsstring *)readfile { //创建文件管理器 nsfilemanager *filemanager = [nsfilemanager defaultmanager]; //获取路径 //参数nsdocumentdirectory要获取那种路径 nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0];//去处需要的路径 //更改到待操作的目录下 [filemanager changecurrentdirectorypath:[documentsdirectory stringbyexpandingtildeinpath]]; //获取文件路径 nsstring *path = [documentsdirectory stringbyappendingpathcomponent:@"username"]; nsdata *reader = [nsdata datawithcontentsoffile:path]; return [[nsstring alloc] initwithdata:reader encoding:nsutf8stringencoding]; } @end
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!