NSFileManager文件的创建复制删除遍历 NSFileHandler读取关闭同步 NSUserDefault(类似android shreapreference) plist属性列表

//
//  main.m
//  FileOperator
//
//  Created by 千雅爸爸 on 16/10/6.
//  Copyright © 2016年 kodulf. All rights reserved.
//

#import <Foundation/Foundation.h>
//使用宏定义
#define kPathAtFileOperation(subpath) \
[NSString stringWithFormat:@"/Users/JiYi2013/Documents/ios/oc day04 review/FileOperator/FileOperator/%@",subpath]

//NSString *mainFolder = @"/Users/JiYi2013/Documents/ios/oc day04 review/FileOperator/FileOperator";

void nsFileManagerTest(){
    // insert code here...
    NSLog(@"Hello, World!");
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //先打开路径,可以直接把路径拖过来,
    NSError *error = nil;
    BOOL ret =[fileManager createDirectoryAtPath:kPathAtFileOperation(@"test2") withIntermediateDirectories:NO attributes:nil error:&error];//二级指针,这里一定要注意,为什么呢,为什么是取地址符号呢?
    if(ret){
        NSLog(@"目录创建成功");
    }else{
        NSLog(@"目录创建失败,reason:%@",error);
    }
    //这里使用YES就是如果中间没有该文件夹的时候创建
    BOOL ret2 = [fileManager createDirectoryAtPath:kPathAtFileOperation(@"test1/test1-1") withIntermediateDirectories:YES attributes:nil error:nil];
    if(ret2){
        NSLog(@"目录创建成功");
    }else{
        NSLog(@"目录创建失败");
    }
    
    ret = [fileManager createFileAtPath:kPathAtFileOperation(@"test.txt") contents:nil attributes:nil];
    if(ret){
        NSLog(@"文件创建成功");
    }else{
        NSLog(@"文件创建失败");
    }
    
    NSString *string = @"文件操作";
    //nsstring 到nsdata的转换
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    ret = [fileManager createFileAtPath:kPathAtFileOperation(@"test.txt") contents:data attributes:nil];
    if(ret){
        NSLog(@"文件创建成功");
    }else{
        NSLog(@"文件创建失败");
    }
    
    //+++++++++文件的遍历+++++++++
    //浅度便利和深度便利
    NSArray *fileArray = [fileManager contentsOfDirectoryAtPath:kPathAtFileOperation(@"") error:nil];
    NSLog(@"浅度便利:当前目录下的文件列表%@",fileArray);
    
    fileArray = [fileManager subpathsAtPath:kPathAtFileOperation(@"")];
    NSLog(@"深度便利:当前目录下的文件列表%@",fileArray);
    //移动操作
    ret = [fileManager moveItemAtPath:kPathAtFileOperation(@"test.txt") toPath:kPathAtFileOperation(@"test1/test1-1/test2.txt") error:nil];
    if(ret){
        NSLog(@"移动操作成功");
    }else{
        NSLog(@"移动操作失败");
    }
    //复制操作,拷贝操作
    ret = [fileManager copyItemAtPath:kPathAtFileOperation(@"test1/test1-1/test2.txt") toPath:kPathAtFileOperation(@"test1/test1-1/test3.txt") error:nil];
    if(ret){
        NSLog(@"拷贝操作成功");
    }else{
        NSLog(@"拷贝操作失败");
    }
    
    //删除操作
    ret = [fileManager removeItemAtPath:kPathAtFileOperation(@"test1/test1-1/test3.txt") error:nil];
    if(ret){
        NSLog(@"删除操作成功");
    }else{
        NSLog(@"删除操作失败");
    }
    
    //查看文件属性:
    NSDictionary *attributes = [fileManager attributesOfItemAtPath:kPathAtFileOperation(@"test1/test1-1/test2.txt") error:nil];
    NSLog(@"文件的属性列表:%@",attributes);
    
    //查看文件在某个目录下是否存在:
    ret = [fileManager fileExistsAtPath:kPathAtFileOperation(@"test1/test1-1/test2.txt")];
    if(ret){
        NSLog(@"文件存在");
    }else{
        NSLog(@"文件不存在");
    }
}

/**文件的读取,写入,关闭和同步使用filehandler来做*/
void nsFileHandlerTest(){
    //通过只读的方式去打开的时候,只能读取
    NSFileHandle *fileHandler = [NSFileHandle fileHandleForReadingAtPath:kPathAtFileOperation(@"test1/test1-1/test2.txt")];
    //读取到文件的末尾,
    NSLog(@"文件内容:%@",[fileHandler readDataToEndOfFile]);
    //注意读了以后光标就在最后一个了,如果继续读,会没有数据
    NSLog(@"注意读了以后光标就在最后一个了,如果继续读,会没有数据,继续文件内容:%@",[fileHandler readDataToEndOfFile]);
    //打开以后一定要关闭,不然会很占用内存
    [fileHandler closeFile];
    
    //打开一个可写的
    fileHandler = [NSFileHandle fileHandleForWritingAtPath:kPathAtFileOperation(@"test1/test1-1/test2.txt")];
    [fileHandler closeFile];
    
    //使用更新去打开
    fileHandler =[NSFileHandle fileHandleForUpdatingAtPath:kPathAtFileOperation(@"test1/test1-1/test2.txt")];
    NSLog(@"文件内容:%@",[fileHandler readDataToEndOfFile]);
    [fileHandler closeFile];
    
    //还可以读取指定的长度的
    fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:kPathAtFileOperation(@"test1/test1-1/test2.txt")];
    NSLog(@"获取指定长度的字节的数据:%@",[fileHandler readDataOfLength:1]);
    [fileHandler closeFile];
    
    //解决读取后光标到了最后不能再读区的问题,
    fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:kPathAtFileOperation(@"test1/test1-1/test2.txt")];
    NSLog(@"第一次读取%@",[fileHandler readDataToEndOfFile]);
    NSLog(@"第二次读取%@",[fileHandler readDataToEndOfFile]);
    [fileHandler seekToFileOffset:0];
    NSLog(@"光标返回后,第三次读取%@",[fileHandler readDataToEndOfFile]);
    
    [fileHandler closeFile];
    
    //写入数据
    NSString *baidu = @"写入的数据 baidu ";
    NSData *add = [baidu dataUsingEncoding:NSUTF8StringEncoding];
    fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:kPathAtFileOperation(@"test1/test1-1/test2.txt")];
    [fileHandler writeData:add];
    [fileHandler seekToFileOffset:0];
    NSLog(@"添加以后%@",[fileHandler readDataToEndOfFile]);
    [fileHandler closeFile];
    
    //文件的同步
    fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:kPathAtFileOperation(@"test1/test1-1/test2.txt")];
    [fileHandler synchronizeFile];
    [fileHandler closeFile];

}

void plistTest(){
    //plist文件又叫属性列表,实际上一个xml格式的数据,可以使用文本编辑器去打开就可以看到了
    //可以是数组或者是字典
    //路径/Users/JiYi2013/Documents/ios/oc day04 review/FileOperator/FileOperator/
    
    //使用数组:
    NSArray *array = @[@"a",@"b",@"c"];
    //写入到文件中,第二个参数表示的是如果是yes的话,会把当前的所有的东西,先写到一个副本中,然后当写完了再拷贝回来,
    [array writeToFile: kPathAtFileOperation(@"test.plist") atomically:YES];
    
    //使用字典:
    NSDictionary *dic = @{@"name":@"kodulf",@"age":@29};
    [dic writeToFile:kPathAtFileOperation(@"test2.plist") atomically:YES];
    
    //从plist中获取数据,如果是数组就要用数组接受,如果是字典呀用字典接受
    NSArray *result =[NSArray arrayWithContentsOfFile:kPathAtFileOperation(@"test.plist")];
    NSLog(@"从数组的plist文件中读取%@",result);
    
    NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:kPathAtFileOperation(@"test.plist")];
    NSLog(@"从字典的plist文件中读取%@",resultDic);
    
}

//和java中的preference一样,是持久化存取的。
void nsUserDefaultTest(){
    //用户设置的读取和设置
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSString *string = @"www.baidu.com";
    //添加
    [defaults setObject:string forKey:@"webset"];
    [defaults synchronize];//同步一下
    NSString *resultString = [defaults objectForKey:@"webset"];
    NSLog(@"result : %@",resultString);
    
    //将所有的键值对都打印出来
    NSDictionary *dic = [defaults dictionaryRepresentation];
    NSLog(@"用户默认的设置的所有的键值对:%@",dic);
    //移除
    [defaults removeObjectForKey:@"webset"];
    
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        nsFileManagerTest();
        
        nsFileHandlerTest();
        
        plistTest();
        
        nsUserDefaultTest();
        
    }
    return 0;
}



















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值