iPhone开发-创建、读取、写入文件 今天的东西是如何在iPhoneOS下创建、删除、读取、写入文件 创建与删除: //创建文件管理器 NSFileManager *fileManager =
2013-12-19 19:52
691 查看
iPhone开发-创建、读取、写入文件
今天的东西是如何在iPhoneOS下创建、删除、读取、写入文件
创建与删除:
//创建文件管理器
NSFileManager *fileManager = [NSFileManagerdefaultManager];
//获取路径
//参数NSDocumentDirectory要获取那种路径
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsobjectAtIndex:0];//去处需要的路径
//更改到待操作的目录下
[fileManagerchangeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
[fileManagercreateFileAtPath:@"fileName" contents:nilattributes:nil];
//删除待删除的文件
[fileManagerremoveItemAtPath:@"createdNewFile" error:nil];
写入数据:
//获取文件路径
NSString *path = [documentsDirectorystringByAppendingPathComponent:@"fileName"];
//待写入的数据
NSString *temp = @"Hellofriend";
int data0 =100000;
float data1 =23.45f;
//创建数据缓冲
NSMutableData *writer = [[NSMutableDataalloc] init];
//将字符串添加到缓冲中
[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
//将其他数据添加到缓冲中
[writer appendBytes:&data0length:sizeof(data0)];
[writer appendBytes:&data1length:sizeof(data1)];
//将缓冲的数据写入到文件中
[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:&gData0range:NSMakeRange([templength], sizeof(gData0))];
[reader getBytes:&gData2range:NSMakeRange([templength] + sizeof(gData0),sizeof(gData1))];
NSLog(@"gData0:%@ gData1:%i gData2:%f", gData0, gData1,gData2);
读取工程中的文件:
读取数据时,要看待读取的文件原有的文件格式,是字节码还是文本,我经常需要重文件中读取字节码,所以我写的是读取字节文件的方式。
//用于存放数据的变量,因为是字节,所以是UInt8
UInt8 b = 0;
//获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@""];
//获取数据
NSData *reader = [NSDatadataWithContentsOfFile:path];
//获取字节的个数
int length = [readerlength];
NSLog(@"------->bytesLength:%d",length);
for(int i =0; i < length; i++)
{
//读取数据
[reader getBytes:&b range:NSMakeRange(i,sizeof(b))];
NSLog(@"-------->data%d:%d",i, b);
}
前言:接上篇博客,把对象写入文件,存入内存。那么,我们一般把需要保存的文件存放在什么位置了?沙盒中的Documetnts!
正文:先附上自己的代码
此段代码创建的filePath为
/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3/Documents/imageViews/test.plist
一、创建~/Documents/imageViews/test.plist的详细步骤
1、找到Documetnts目录所在的位置
str1为/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3
2、加上Documetnts这层目录
path为/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3/Documents
注意:使用的是stringByAppendingPathComponent:方法,不需要加“/"; 如果用的是别的方法加的话,需要写成@"/Documents".
3、在Documetnts目录中创建名为imageViews的目录
创建NSFileManager的实例fileManager.NSFileManager这个类是专门进行文件管理的,可以创建文件,目录,删除,遍历目录等。我们调用了 createDirectoryAtPath:方法创建了一个新目录。
4、在imageViews目录里,创建文件test.plist.
调用createFileAtPath:创建文件
最后得到的整个文件路径为:
二、NSFileManager常用的文件管理操作
1、创建目录 createDirectoryAtPath:
2、创建文件 createFileAtPath:
3、删除某个文件 removeItemAtPath:
4、检查某个文件是否存在 fileExistsAtPath:
5、检查文件是否可读 isReadableFileAtPath:
是否可写:isWritableFileAtPath:
6、取得文件属性 fileAttributesAtPath:
改变文件属性changeAttributesAtPath:
7、从path代表的文件中读取数据:contentsAtPath
8、移动文件movePath:toPath:handler:
9、复制文件copyPath:toPath:handler:
更多0
今天的东西是如何在iPhoneOS下创建、删除、读取、写入文件
创建与删除:
//创建文件管理器
NSFileManager *fileManager = [NSFileManagerdefaultManager];
//获取路径
//参数NSDocumentDirectory要获取那种路径
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsobjectAtIndex:0];//去处需要的路径
//更改到待操作的目录下
[fileManagerchangeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
[fileManagercreateFileAtPath:@"fileName" contents:nilattributes:nil];
//删除待删除的文件
[fileManagerremoveItemAtPath:@"createdNewFile" error:nil];
写入数据:
//获取文件路径
NSString *path = [documentsDirectorystringByAppendingPathComponent:@"fileName"];
//待写入的数据
NSString *temp = @"Hellofriend";
int data0 =100000;
float data1 =23.45f;
//创建数据缓冲
NSMutableData *writer = [[NSMutableDataalloc] init];
//将字符串添加到缓冲中
[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
//将其他数据添加到缓冲中
[writer appendBytes:&data0length:sizeof(data0)];
[writer appendBytes:&data1length:sizeof(data1)];
//将缓冲的数据写入到文件中
[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:&gData0range:NSMakeRange([templength], sizeof(gData0))];
[reader getBytes:&gData2range:NSMakeRange([templength] + sizeof(gData0),sizeof(gData1))];
NSLog(@"gData0:%@ gData1:%i gData2:%f", gData0, gData1,gData2);
读取工程中的文件:
读取数据时,要看待读取的文件原有的文件格式,是字节码还是文本,我经常需要重文件中读取字节码,所以我写的是读取字节文件的方式。
//用于存放数据的变量,因为是字节,所以是UInt8
UInt8 b = 0;
//获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@""];
//获取数据
NSData *reader = [NSDatadataWithContentsOfFile:path];
//获取字节的个数
int length = [readerlength];
NSLog(@"------->bytesLength:%d",length);
for(int i =0; i < length; i++)
{
//读取数据
[reader getBytes:&b range:NSMakeRange(i,sizeof(b))];
NSLog(@"-------->data%d:%d",i, b);
}
ios NSFileManager创建目录、文件
前言:接上篇博客,把对象写入文件,存入内存。那么,我们一般把需要保存的文件存放在什么位置了?沙盒中的Documetnts!正文:先附上自己的代码
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *str1 = NSHomeDirectory(); _filePath = [[NSString stringWithFormat:@"%@/Documents/imageViews/test.plist",str1]retain]; NSLog(@"%@",_filePath); if(![fileManager fileExistsAtPath:_filePath]){//如果不存在,则说明是第一次运行这个程序,那么建立这个文件夹 NSLog(@"first run"); NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString *directryPath = [path stringByAppendingPathComponent:@"imageViews"]; [fileManager createDirectoryAtPath:directryPath withIntermediateDirectories:YES attributes:nil error:nil]; NSString *filePath = [directryPath stringByAppendingPathComponent:@"test.plist"]; NSLog(@"%@",filePath); [fileManager createFileAtPath:filePath contents:nil attributes:nil]; ......... }
此段代码创建的filePath为
/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3/Documents/imageViews/test.plist
一、创建~/Documents/imageViews/test.plist的详细步骤
1、找到Documetnts目录所在的位置
NSString *str1 = NSHomeDirectory();
str1为/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3
2、加上Documetnts这层目录
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
path为/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3/Documents
注意:使用的是stringByAppendingPathComponent:方法,不需要加“/"; 如果用的是别的方法加的话,需要写成@"/Documents".
3、在Documetnts目录中创建名为imageViews的目录
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *directryPath = [path stringByAppendingPathComponent:@"imageViews"]; [fileManager createDirectoryAtPath:directryPath withIntermediateDirectories:YES attributes:nil error:nil];
创建NSFileManager的实例fileManager.NSFileManager这个类是专门进行文件管理的,可以创建文件,目录,删除,遍历目录等。我们调用了 createDirectoryAtPath:方法创建了一个新目录。
4、在imageViews目录里,创建文件test.plist.
NSString *filePath = [directryPath stringByAppendingPathComponent:@"test.plist"]; [fileManager createFileAtPath:filePath contents:nil attributes:nil];
调用createFileAtPath:创建文件
最后得到的整个文件路径为:
二、NSFileManager常用的文件管理操作
1、创建目录 createDirectoryAtPath:
2、创建文件 createFileAtPath:
3、删除某个文件 removeItemAtPath:
4、检查某个文件是否存在 fileExistsAtPath:
5、检查文件是否可读 isReadableFileAtPath:
是否可写:isWritableFileAtPath:
6、取得文件属性 fileAttributesAtPath:
改变文件属性changeAttributesAtPath:
7、从path代表的文件中读取数据:contentsAtPath
8、移动文件movePath:toPath:handler:
9、复制文件copyPath:toPath:handler:
更多0
相关文章推荐
- (转)40多个关于人脸检测/识别的API、库和软件
- JAVA异常机制介绍/如何正确的进行JAVA异常处理
- Struts注解,Spring注解,Hibernate注解
- 计算机网络学习-传输层
- 菜鸟Android学习之路14——带图标的ListView组件
- OCR学习之一:OCR简介
- 看看机器学习都有哪些比较成熟的应用
- Android(2)—Mono For Android App版本自动更新
- fafu 1411
- myibatis--增删查改
- Ftp介绍及视频教程
- java学习笔记01
- [Sparrow OS 设计文档连载(一)] Introduction
- textField的过滤筛选
- startActivityForResul的用法
- Deep Learning Tutorials
- Protocol Buffers
- 手把手教你使用FineUI开发一个b/s结构的取送货管理信息系统(附源码+视频教程(第5节))
- Protocol Buffers
- 八 个优秀的 jQuery Mobile 教程