码迷,mamicode.com
首页 > 移动开发 > 详细

ios文件操作

时间:2014-09-28 17:52:43      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   strong   for   

IOS访问沙盒目录API

获取程序目录

  NSString *homePath = NSHomeDirectory();

 ~/Library/Application Support/iPhone Simulator/7.1/Applications/66095245-FD69-40D5-B3F5-9594A6DC6862

 

获取Documents  Library  tmp  Library/Cache目录

    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array firstObject];
    NSLog(@"%@",documentPath);
    
    //Cache目录
    array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesPath = [array firstObject];
    NSLog(@"%@",cachesPath);
    
    //Library
    array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
    NSString *libraryPath = [array firstObject];
    NSLog(@"%@",libraryPath);
    
    //tmp
    NSString *tmpPath = NSTemporaryDirectory();
    NSLog(@"%@",tmpPath);

 

数组在文件中的读写

   //写入数组至文件
    NSArray *writeArray = @[@"张三",@"李四",@"王五"];
    [writeArray writeToFile:[documentPath stringByAppendingPathComponent:@"test.txt"] atomically:YES];
    
    
    //读取文件到数组
    NSArray *readArray = [NSArray arrayWithContentsOfFile:[documentPath stringByAppendingPathComponent:@"test.txt"]];

atomically:如果为YES保证文件的写入原子性,会先创建一个临时文件,直到文件内容写入成功再导入到目标文件里.

如果为NO,则直接写入目标文件里.

 

通过NSFileManager进行文件操作

通过NSFileManager进行文件的创建

  //在Document下创建文件夹和文件
    NSString *testDirectory = [documentPath stringByAppendingPathComponent:@"test"];
    
    NSString *test1 = [testDirectory stringByAppendingPathComponent:@"test1.txt"];
    NSString *test2 = [testDirectory stringByAppendingPathComponent:@"test2.txt"];
    NSString *test3 = [testDirectory stringByAppendingPathComponent:@"test3.txt"];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    
    NSString *text = @"你好,IOS";
    [manager createFileAtPath:test1 contents:[text dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    [manager createFileAtPath:test2 contents:[text dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    [manager createFileAtPath:test3 contents:[text dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

 

通过NSFileManager进行文件的遍历

  //读取所有的文件名,两种方法效果一样
    NSArray *files1 = [manager subpathsAtPath:testDirectory];
    NSArray *files2 = [manager subpathsOfDirectoryAtPath:testDirectory error:nil];

 

通过NSFileManager操作当前目录

  [manager changeCurrentDirectoryPath:documentPath];
    NSArray *fileArray = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1", @"hello world2",nil];
    [manager createFileAtPath:@"testFileNSFileManager.txt" contents:fileArray attributes:nil];
    [manager createFileAtPath:@"testFileNSFileManager1.txt" contents:fileArray attributes:nil];

 

通过NSFileManager删除文件

  [manager removeItemAtPath:@"testFileNSFileManager1.txt" error:nil];

 

通过NSFileHandle进行文件IO操作 

NSFileHandle不能进行文件的创建,所以文件创建还得通过NSFileManager,NSFileHandle打开的文件如果不存在将返回nil

  //在Document下创建三个文件,将文件1全部内容和文件2部分内容复制到文件3,然后删除文件1;
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array firstObject];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSString *path1 = [documentPath stringByAppendingPathComponent:@"text1.txt"];
    NSString *path2 = [documentPath stringByAppendingPathComponent:@"text2.txt"];
    NSString *path3 = [documentPath stringByAppendingPathComponent:@"text3.txt"];
    
    //创建3个文件
    [manager createFileAtPath:path1 contents:[@"hello from text1" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    [manager createFileAtPath:path2 contents:[@"hello from text2" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    [manager createFileAtPath:path3 contents:[@"hello from text3" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    
    
    //获取NSFileHandle对象
    NSFileHandle *path1Handle = [NSFileHandle fileHandleForReadingAtPath:path1];
    NSFileHandle *path2Handle = [NSFileHandle fileHandleForReadingAtPath:path2];
    NSFileHandle *path3Handle = [NSFileHandle fileHandleForWritingAtPath:path3];
    
    
    //IO读写
    NSData *text1Data = [path1Handle readDataToEndOfFile];
    NSData *text2Data = [path2Handle readDataOfLength:5];
    //将text3文件的文件指针移动到最后
    [path3Handle seekToEndOfFile];
    
    NSMutableData *data = [NSMutableData dataWithData:text1Data];
    [data appendData:text2Data];
    [path3Handle writeData:data];
    
    //关闭NSFileHandle对象
    [path1Handle closeFile];
    [path2Handle closeFile];
    [path3Handle closeFile];
    
    //删除text1
    [manager removeItemAtPath:path1 error:nil];

 

 NSBundle处理项目相关资源

 http://blog.csdn.net/iphoneing/article/details/5872610

 

ios文件操作

标签:style   blog   http   color   io   os   ar   strong   for   

原文地址:http://www.cnblogs.com/zanglitao/p/3998295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!