OC 文件管理 NSFileManager NSFileHandle 归档和解档

  //输出文件路径 跟目录

    NSString *stringPath = NSHomeDirectory();

    NSLog(@"%@",stringPath);

    

   //取出文件路径的组成部分

    NSLog(@"%@",[stringPath pathComponents]);

    //取出文件路径的最后一个组成部分

    NSLog(@"%@",[stringPath lastPathComponent]);

    

    //给文件路径追加一部分路径

    NSLog(@"%@",[stringPath stringByAppendingPathComponent:@"te.text"]);

    

  


    

    

    //-----------------------文件管理

    //创建一个文件管理 fileManager

    NSFileManager *fileManager = [NSFileManager defaultManager];

    

    //获取当前路径

    NSLog(@"当前路径为:%@",[fileManager currentDirectoryPath]);

    //创建文件内容 NSString类型

    NSString *fileString = @"listFlie";

    

    //将字符串类型转换为NSData类型 编码格式为NSUTF8

    NSData *fileData = [fileString dataUsingEncoding:NSUTF8StringEncoding];

    

    //创建文件  方法createFileAtPath 返回BOOL类型 if判断

    if ([fileManager createFileAtPath:[stringPath stringByAppendingPathComponent:@"list.text"] contents:fileData attributes:nil]) {

        NSLog(@"文件创建成功");

    }

    

    //读取文件 contentsAtPath: 返回NSData类型

    NSData *contentData = [fileManager contentsAtPath:[stringPath stringByAppendingPathComponent:@"list.text"]];

    //NSData类型转转换为NSString类型

    NSString *contentString = [[NSString alloc]initWithData:contentData encoding:NSUTF8StringEncoding];

    //读取文件内容

    NSLog(@"读取文件内容 :%@",contentString);

    

    //复制文件

    [fileManager copyItemAtPath:[stringPath stringByAppendingPathComponent:@"list.text"] toPath: @"/Users/a11/desktop/test.txt" error:nil];

    

    //移动文件

    [fileManager moveItemAtPath:[stringPath stringByAppendingPathComponent:@"list.text"] toPath:@"/Users/a11/desktop/test.txt" error:nil];

    

    //删除文件

    [fileManager removeItemAtPath:@"/Users/a11/desktop/test.txt" error:nil];

    

   //创建文件目录 也就是创建文件夹

   if ([fileManager createDirectoryAtPath:[stringPath stringByAppendingPathComponent:@"newFile.text"] withIntermediateDirectories:YES attributes:nil error:nil])

    {

        NSLog(@"文件目录创建成功");

    }

   

    //读取文件目录的内容

    [fileManager contentsOfDirectoryAtPath:[stringPath stringByAppendingPathComponent:@"newFile.text"] error:nil];

    

    //--------NSFileHandle-------

    //创建一个NSFileHandle

    NSFileHandle *fileHandle = nil;

    

    //指定要操作文件的路径 更新

    fileHandle=[NSFileHandle fileHandleForWritingAtPath :@"/Users/a11/Desktop/we.txt"];

    

    //指定光标在文件内容末尾

    [fileHandle seekToEndOfFile];

    

    //创建需要修改的内容

    NSString *appendString = @"杨茹";

    

    //转换成数据流格式

    NSData *appendData = [appendString dataUsingEncoding:NSUTF8StringEncoding];

    

    //写入到文件中

    [fileHandle writeData:appendData];

    

    //关闭文件

    [fileHandle closeFile];



 //--------归档-------

    //创建一个可变数据流

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

    

    //把用归档格式的数据传值给可变大小的文件流

    NSKeyedArchiver *archiever = [[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData];

    

    //把集合数据以归档形式编码 键只能是NSString类型 Objectid类型

    [archiever encodeObject:@"80"forKey:@"age"];

    [archiever encodeObject:@"jack"forKey:@"name"];

    

    //完成归档

    [archiever finishEncoding];

    

    //把归档过来的可变大小的数据流写入文件中 永久存储

    [mutableData writeToFile:@"/Users/a11/Desktop/we.txt" atomically:YES];

    

    

    //--------解档-------

    //创建一个NSFileManager类型的对象

    NSFileManager *fileManager = [NSFileManager defaultManager];

    //NSData文件流接受从PATH路径读取出来的文件内容

    NSData *data = [fileManager contentsAtPath:@"/Users/a11/Desktop/we.txt"];

    

    //实例化一个解档对象 注意:初始化为解档格式并要放入要解档的数据流

    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];

    

    //输入要解档的Key  返回的时数组类型

    NSArray *arrayUnArchiver = [unArchiver decodeObjectForKey:@"age"];

    

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


其他一些文件操作的方法:

NSFileManager 常⽤用⽂文件操作⽅方法

-(BOOL)contentsAtPath:path 从⽂文件中读取数据

-(BOOL)createFileAtPath:path contents:(BOOL)data attributes:向一个文件写⼊入数据

-(BOOL)removeFileAtPath: path handler: handler 删除⼀一个⽂文件

-(BOOL)movePath: from toPath: to handler: handler 重命名或移动⼀一个⽂文件(to可能已经存在)

-(BOOL)copyPath:from toPath:to handler: handler 复制⽂文件 (to不能 在)

-(BOOL)contentsEqualAtPath:path1 andPath:path2 ⽐比较两个⽂文件的内容

-(BOOL)fileExistsAtPath:path 测试⽂文件是否存在

-(BOOL)isReadablefileAtPath:path 测试⽂文件是否存在,且是否能执⾏行读操作

-(BOOL)isWritablefileAtPath:path 测试⽂文件是否存在,且是否能执⾏行写操作

-(NSDictionary *)fileAttributesAtPath:path traverseLink:(BOOL)flag 获取⽂文件的属性

-(BOOL)changeFileAttributes:attr atPath:path 更改⽂文件的属性 

(NSString *)currentDirectoryPath 获取当前目录

-(BOOL)changeCurrentDirectoryPath:path 更改当前目录

-(BOOL)copyPath:from toPath:to handler:handler 复制目录结构,to不能已经存在

-(BOOL)createDirectoryAtPath:path attributes:attr 创建目录

-(BOOL)fileExistsAtPath:path isDirectory:(BOOL *)flag 测试⽂文件是否为目录 (flag存储结构yes/no)

-(NSArray *)contentsOfDirectoryAtPath:path 列出目录的内容

-(NSDirectoryEnumerator *)enumeratorAtPath:path 枚举目录的内容

-(BOOL)removeFileAtPath:path handler:handler 删除空目录

-(BOOL)movePath:from toPath:to handler:handler 重命名或移动⼀个目录,to不能是已经存在的 


常⽤用NSFileHandle⽅方法

(NSFileHandle *)fileHandleForReadingAtPath:path 打开一个文件准备读取

(NSFileHandle *)fileHandleForWritingAtPath:path 打开一个文件准备写入

(NSFileHandle *)fileHandleForUpdatingAtPath:path 打开一个文件准备更新(读取和写⼊)

-(NSData *)availableData 从设备或通道返回可用数据

-(NSData *)readDataToEndOfFile 读取其余的数据直到文件末尾(最多UINT_MAX字节) 

-(NSData *)readDataOfLength:(unsigned int)bytes 从⽂件读取指定数目bytes的内容

-(void)writeData:data data写⼊文件

-(unsigned long long) offsetInFile 获取当前⽂件的偏移量

-(void)seekToFileOffset:offset 设置当前⽂件的偏移量

-(unsigned long long) seekToEndOfFile 将当前⽂件的偏移量定位的⽂件末尾

-(void)truncateFileAtOffset:offset 将⽂件的长度设置为offset字节

-(void)closeFile 关闭文件 








转载于:https://my.oschina.net/u/2483162/blog/528590

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值