首页 技术 正文
技术 2022年11月9日
0 收藏 333 点赞 3,999 浏览 2602 个字

一、NSFileManager:1.1、获取NSFileManagerNSFileManager *manager = [NSFileManager defaultManager];     NSFileManager是单例模式,所以不能使用alloc+init创建   

1.2、文件操作

NSString *filePath = [homePath stringByAppendingPathComponent:@”Documents/file.text”];  //创建路径NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];   编码数据BOOL sucess = [manager createFileAtPath:filePath contents:data attributes:nil]; 创建文件,参数:文件路径、文件内容、文件的属性NSData *datas = [manager contentsAtPath:filePath];  根据路径读取文件NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];   将文件数据转换成stringBOOL sucess2 = [manager moveItemAtPath:filePath toPath:targetPath error:nil]  移动文件,移动的同时可以重命名文件名BOOL sucess3 = [manager copyItemAtPath:filePath toPath:targetPath error:nil]; 复制文件BOOL isExist = [manager fileExistsAtPath:filePath];//判断文件是否存在  BOOL sucess4 = [manager removeItemAtPath:filePath error:nil];   删除文件NSDictionary *dic = [manager attributesOfItemAtPath:filePath error:nil];  获取文件属性BOOL success = [manager changeFileAttributes:attr atPath:path]                        更改文件的属性 BOOL success = [manager isReadablefileAtPath:path] 测试文件是否存在,且是否能执行读操作 BOOL success = [manager isWritablefileAtPath:path] 测试文件是否存在,且是否能执行写操作 

1.3、目录操作

BOOL success1 = [manager createDirectoryAtPath:filePaths withIntermediateDirectories:YES attributes:nil error:&error]; 创建文件夹-(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:

2.1 获取NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];    获取文件准备写入                                   (NSFileHandle *)fileHandleForReadingAtPath:path                        打开一个文件准备读取                                   (NSFileHandle *)fileHandleForUpdatingAtPath:path                        打开一个文件准备更新(读取和写入)FileSize = [[attributes objectForKey:NSFileSize] intValue]    获取文件大小[handle seekToEndOfFile]; 将游标链接到文件尾部[handle writeData:data];    在尾部继续写入数据[handle closeFile];   关闭文件 -(NSData *)readDataToEndOfFile                                            读取其余的数据直到文件末尾(最多UINT_MAX字节)-(NSData *)readDataOfLength:(unsigned int)bytes 从文件读取指定数目bytes的内容 -(unsigned long long) offsetInFile      获取当前文件的偏移量-(void)seekToFileOffset:offset         设置当前文件的偏移量 -(unsigned long long) seekToEndOfFile      将当前文件的偏移量定位的文件末尾-(void)truncateFileAtOffset:offset        将文件的长度设置为offset字节  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918