iPhone开发-创建、读取、写入文件 今天的东西是如何在iPhoneOS下创建、删除、读取、写入文件 创建与删除: //创建文件管理器 NSFileManager *fileManager =

2 篇文章 0 订阅
1 篇文章 0 订阅

iPhone开发-创建、读取、写入文件


今天的东西是如何在iPhoneOS下创建、删除、读取、写入文件 


创建与删除:

//创建文件管理器

NSFileManager *fileManager = [NSFileManagerdefaultManager];

//获取路径

//参数NSDocumentDirectory要获取那种路径

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [pathsobjectAtIndex:0];//去处需要的路径


//更改到待操作的目录下

[fileManagerchangeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];


//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nilattributes文件的属性,初始为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 = [[NSMutableDataallocinit];


//将字符串添加到缓冲中

[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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值