IOS开发 沙盒中的文件操作和文件系统NSFilemanager的操作

1、获取程序的Home目录

      NSString  *path = NSHomeDirectory();

      NSLog(@"path:%@",path);

      打印结果:

 

 

   2012-07-11 11:18:16.291 TestProject[2387:f803] path:/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/BAE91297-A4C6-4DDC-A9DA-7B790B36CE7A

 

 

 

   真机上的目录是:

   2012-06-17 14:25:47.059 IosSandbox[4281:f803] /var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2

   可见,真机上的目录是/var/mobile/Applications/这个目录下的,和模拟器不一样。这个是Home目录,其他的子目录和模拟器一样。


2、获取Document目录

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

   NSString *path [paths objectAtIndex:0]; 

   NSLog(@"path:%@"path); 

   打印结果: 

 

 

   2012-07-11 11:21:22.879 TestProject[2417:f803] path:/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/BAE91297-A4C6-4DDC-A9DA-7B790B36CE7A/Documents


3、获取Cache目录

 

   NSArray *paths NSSearchPathForDirectoriesInDomains(NSCachesDirectoryNSUserDomainMask, YES); 

   NSString *path [paths objectAtIndex:0]; 

   NSLog(@"path:%@"path); 

   打印结果:

 

   2012-07-11 11:13:36.162 TestProject[2310:f803] path:/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/BAE91297-A4C6-4DDC-A9DA-7B790B36CE7A/Library/Caches


4、获取Library目录

   NSArray *paths NSSearchPathForDirectoriesInDomains(NSLibraryDirectoryNSUserDomainMask, YES); 

 

 

 

   NSString *path [paths objectAtIndex:0]; 

 

 

 

   NSLog(@"path:%@"path);

 

 

 

   打印结果:

 

 

   2012-07-11 11:14:41.138 TestProject[2337:f803] path:/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/BAE91297-A4C6-4DDC-A9DA-7B790B36CE7A/Library

 

 

5、获取Tmp目录

 

   NSString *path NSTemporaryDirectory(); 

   NSLog(@"%@", path);

   打印结果:

 

 

 

 

 

 

  2012-07-11 11:16:09.438 TestProject[2358:f803] path:/var/folders/hj/8sgyk0f555l1z_n95p2b2kp00000gn/T/

     

 


 

 

 

6、写入文件

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

   NSString *docDir = [paths objectAtIndex:0];

   if(!docDir) {

        NSLog(@"Documents 目录未找到");

   }

   NSArray *array = [[NSArray alloc] initWithObjects:@"内容",@"content",nil];

   NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];

   [array writeToFile:filePath atomically:YES];


7、读取文件

 

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

   NSString *docDir = [paths objectAtIndex:0];

   NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];

   NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];

   NSLog(@"%@",array);


8、NSFilemanager操作

NSError *error;

    

    

    //Documents目录下创建test目录

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

    NSLog(@"%@",path);

    

    NSString *documentsDirectory = [path objectAtIndex:0];

    NSLog(@"%@",documentsDirectory);

    NSFileManager *fileMgr = [NSFileManager defaultManager];

    NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];

    [fileMgr createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:&error];

    

    

    //test目录下创建文件

    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test00.txt"];

    NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test22.txt"];

    NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test33.txt"];

    

    

    NSString *string = @"这是要写入的内容";

    [fileMgr createFileAtPath:testPath contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

    [fileMgr createFileAtPath:testPath2 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

    [fileMgr createFileAtPath:testPath3 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

    

    //获取目录列里所有文件名,两个方法都可以实现打印隐藏文件

    NSArray *files = [fileMgr subpathsOfDirectoryAtPath:testDirectory error:&error];

    NSLog(@"%@",files);

    

    NSArray *files2 = [fileMgr subpathsAtPath:testDirectory];

    NSLog(@"%@",files2);

    

    

    //fileManager使用操作当前目录

    [fileMgr changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];

    //创建文件名称,文件内容,attributes文件属性

    NSString *fileName = @"testFileNSFileManager.txt";

    NSArray *array = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1",@"hello world2", nil];

    [fileMgr createFileAtPath:fileName contents:array attributes:nil];

    

    

    //删除文件

    //[fileMgr removeItemAtPath:fileName error:&error];

    

    

    

    //混合数据的写入

    //获取文件路径

    NSString *writePath = [documentsDirectory stringByAppendingPathComponent:fileName];

    //待写入的内容

    NSString *temp = @"hello,world";

    int dataInt = 1234;

    float dataFloat = 3.14f;

    //创建数据缓冲

    NSMutableData *writer = [[NSMutableData alloc] init];

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

    [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];

    //将其他数据添加到缓冲中

    [writer appendBytes:&dataInt length:sizeof(dataInt)];

    [writer appendBytes:&dataFloat length:sizeof(dataFloat)];

    //将缓冲的数据写入到文件中

    [writer writeToFile:writePath atomically:YES];

    

    

    //混合数据的读取

    

    int intData;

    float floatData = 0.0;

    NSString *stringData;

    

    NSData *reader = [NSData dataWithContentsOfFile:writePath];

    stringData = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])] encoding:NSUTF8StringEncoding];

    [reader getBytes:&intData range:NSMakeRange([temp length], sizeof(intData))];

    [reader getBytes:&floatData range:NSMakeRange([temp length] + sizeof(intData), sizeof(floatData))];

    NSLog(@"stringData:%@ intData:%d floatData:%f", stringData, intData, floatData);




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值